home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / binutils.7 / binutils / binutils-2.7 / gas / config / obj-coff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-04  |  106.3 KB  |  4,220 lines

  1. /* coff object file format
  2.    Copyright (C) 1989, 90, 91, 92, 93, 94, 95, 1996
  3.    Free Software Foundation, Inc.
  4.  
  5.    This file is part of GAS.
  6.  
  7.    GAS is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 2, or (at your option)
  10.    any later version.
  11.  
  12.    GAS is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with GAS; see the file COPYING.  If not, write to the Free
  19.    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  20.    02111-1307, USA.  */
  21.  
  22. #include "as.h"
  23. #include "obstack.h"
  24. #include "subsegs.h"
  25. #include "libiberty.h"
  26.  
  27. /* I think this is probably always correct.  */
  28. #ifndef KEEP_RELOC_INFO
  29. #define KEEP_RELOC_INFO
  30. #endif
  31.  
  32. const char *s_get_name PARAMS ((symbolS * s));
  33. static symbolS *def_symbol_in_progress;
  34.  
  35.  
  36. /* stack stuff */
  37. typedef struct
  38.   {
  39.     unsigned long chunk_size;
  40.     unsigned long element_size;
  41.     unsigned long size;
  42.     char *data;
  43.     unsigned long pointer;
  44.   }
  45. stack;
  46.  
  47. static stack *
  48. stack_init (chunk_size, element_size)
  49.      unsigned long chunk_size;
  50.      unsigned long element_size;
  51. {
  52.   stack *st;
  53.  
  54.   st = (stack *) malloc (sizeof (stack));
  55.   if (!st)
  56.     return 0;
  57.   st->data = malloc (chunk_size);
  58.   if (!st->data)
  59.     {
  60.       free (st);
  61.       return 0;
  62.     }
  63.   st->pointer = 0;
  64.   st->size = chunk_size;
  65.   st->chunk_size = chunk_size;
  66.   st->element_size = element_size;
  67.   return st;
  68. }
  69.  
  70. #if 0
  71. /* Not currently used.  */
  72. static void
  73. stack_delete (st)
  74.      stack *st;
  75. {
  76.   free (st->data);
  77.   free (st);
  78. }
  79. #endif
  80.  
  81. static char *
  82. stack_push (st, element)
  83.      stack *st;
  84.      char *element;
  85. {
  86.   if (st->pointer + st->element_size >= st->size)
  87.     {
  88.       st->size += st->chunk_size;
  89.       if ((st->data = xrealloc (st->data, st->size)) == (char *) 0)
  90.     return (char *) 0;
  91.     }
  92.   memcpy (st->data + st->pointer, element, st->element_size);
  93.   st->pointer += st->element_size;
  94.   return st->data + st->pointer;
  95. }
  96.  
  97. static char *
  98. stack_pop (st)
  99.      stack *st;
  100. {
  101.   if (st->pointer < st->element_size)
  102.     {
  103.       st->pointer = 0;
  104.       return (char *) 0;
  105.     }
  106.   st->pointer -= st->element_size;
  107.   return st->data + st->pointer;
  108. }
  109.  
  110. /*
  111.  * Maintain a list of the tagnames of the structres.
  112.  */
  113.  
  114. static struct hash_control *tag_hash;
  115.  
  116. static void
  117. tag_init ()
  118. {
  119.   tag_hash = hash_new ();
  120. }
  121.  
  122. static void
  123. tag_insert (name, symbolP)
  124.      const char *name;
  125.      symbolS *symbolP;
  126. {
  127.   const char *error_string;
  128.  
  129.   if ((error_string = hash_jam (tag_hash, name, (char *) symbolP)))
  130.     {
  131.       as_fatal ("Inserting \"%s\" into structure table failed: %s",
  132.         name, error_string);
  133.     }
  134. }
  135.  
  136. static symbolS *
  137. tag_find (name)
  138.      char *name;
  139. {
  140. #ifdef STRIP_UNDERSCORE
  141.   if (*name == '_')
  142.     name++;
  143. #endif /* STRIP_UNDERSCORE */
  144.   return (symbolS *) hash_find (tag_hash, name);
  145. }
  146.  
  147. static symbolS *
  148. tag_find_or_make (name)
  149.      char *name;
  150. {
  151.   symbolS *symbolP;
  152.  
  153.   if ((symbolP = tag_find (name)) == NULL)
  154.     {
  155.       symbolP = symbol_new (name, undefined_section,
  156.                 0, &zero_address_frag);
  157.  
  158.       tag_insert (S_GET_NAME (symbolP), symbolP);
  159. #ifdef BFD_ASSEMBLER
  160.       symbol_table_insert (symbolP);
  161. #endif
  162.     }                /* not found */
  163.  
  164.   return symbolP;
  165. }
  166.  
  167.  
  168.  
  169. #ifdef BFD_ASSEMBLER
  170.  
  171. static void SA_SET_SYM_TAGNDX PARAMS ((symbolS *, symbolS *));
  172.  
  173. #define GET_FILENAME_STRING(X) \
  174. ((char*)(&((X)->sy_symbol.ost_auxent->x_file.x_n.x_offset))[1])
  175.  
  176. /* @@ Ick.  */
  177. static segT
  178. fetch_coff_debug_section ()
  179. {
  180.   static segT debug_section;
  181.   if (!debug_section)
  182.     {
  183.       CONST asymbol *s;
  184.       s = bfd_make_debug_symbol (stdoutput, (char *) 0, 0);
  185.       assert (s != 0);
  186.       debug_section = s->section;
  187.     }
  188.   return debug_section;
  189. }
  190.  
  191. void
  192. SA_SET_SYM_ENDNDX (sym, val)
  193.      symbolS *sym;
  194.      symbolS *val;
  195. {
  196.   combined_entry_type *entry, *p;
  197.  
  198.   entry = &coffsymbol (sym->bsym)->native[1];
  199.   p = coffsymbol (val->bsym)->native;
  200.   entry->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p = p;
  201.   entry->fix_end = 1;
  202. }
  203.  
  204. static void
  205. SA_SET_SYM_TAGNDX (sym, val)
  206.      symbolS *sym;
  207.      symbolS *val;
  208. {
  209.   combined_entry_type *entry, *p;
  210.  
  211.   entry = &coffsymbol (sym->bsym)->native[1];
  212.   p = coffsymbol (val->bsym)->native;
  213.   entry->u.auxent.x_sym.x_tagndx.p = p;
  214.   entry->fix_tag = 1;
  215. }
  216.  
  217. static int
  218. S_GET_DATA_TYPE (sym)
  219.      symbolS *sym;
  220. {
  221.   return coffsymbol (sym->bsym)->native->u.syment.n_type;
  222. }
  223.  
  224. int
  225. S_SET_DATA_TYPE (sym, val)
  226.      symbolS *sym;
  227.      int val;
  228. {
  229.   coffsymbol (sym->bsym)->native->u.syment.n_type = val;
  230.   return val;
  231. }
  232.  
  233. int
  234. S_GET_STORAGE_CLASS (sym)
  235.      symbolS *sym;
  236. {
  237.   return coffsymbol (sym->bsym)->native->u.syment.n_sclass;
  238. }
  239.  
  240. int
  241. S_SET_STORAGE_CLASS (sym, val)
  242.      symbolS *sym;
  243.      int val;
  244. {
  245.   coffsymbol (sym->bsym)->native->u.syment.n_sclass = val;
  246.   return val;
  247. }
  248.  
  249. /* Merge a debug symbol containing debug information into a normal symbol. */
  250.  
  251. void
  252. c_symbol_merge (debug, normal)
  253.      symbolS *debug;
  254.      symbolS *normal;
  255. {
  256.   S_SET_DATA_TYPE (normal, S_GET_DATA_TYPE (debug));
  257.   S_SET_STORAGE_CLASS (normal, S_GET_STORAGE_CLASS (debug));
  258.  
  259.   if (S_GET_NUMBER_AUXILIARY (debug) > S_GET_NUMBER_AUXILIARY (normal))
  260.     /* take the most we have */
  261.     S_SET_NUMBER_AUXILIARY (normal, S_GET_NUMBER_AUXILIARY (debug));
  262.  
  263.   if (S_GET_NUMBER_AUXILIARY (debug) > 0)
  264.     {
  265.       /* Move all the auxiliary information.  */
  266.       /* @@ How many fields do we want to preserve?  Would it make more
  267.      sense to pick and choose those we want to copy?  Should look
  268.      into this further....  [raeburn:19920512.2209EST]  */
  269.       alent *linenos;
  270.       linenos = coffsymbol (normal->bsym)->lineno;
  271.       memcpy ((char *) &coffsymbol (normal->bsym)->native,
  272.           (char *) &coffsymbol (debug->bsym)->native,
  273.           S_GET_NUMBER_AUXILIARY(debug) * AUXESZ);
  274.       coffsymbol (normal->bsym)->lineno = linenos;
  275.     }
  276.  
  277.   /* Move the debug flags. */
  278.   SF_SET_DEBUG_FIELD (normal, SF_GET_DEBUG_FIELD (debug));
  279. }
  280.  
  281. void
  282. c_dot_file_symbol (filename)
  283.      char *filename;
  284. {
  285.   symbolS *symbolP;
  286.  
  287.   symbolP = symbol_new (filename, bfd_abs_section_ptr, 0, &zero_address_frag);
  288.  
  289.   S_SET_STORAGE_CLASS (symbolP, C_FILE);
  290.   S_SET_NUMBER_AUXILIARY (symbolP, 1);
  291.  
  292.   symbolP->bsym->flags = BSF_DEBUGGING;
  293.  
  294. #ifndef NO_LISTING
  295.   {
  296.     extern int listing;
  297.     if (listing)
  298.       {
  299.     listing_source_file (filename);
  300.       }
  301.   }
  302. #endif
  303.  
  304.   /* Make sure that the symbol is first on the symbol chain */
  305.   if (symbol_rootP != symbolP)
  306.     {
  307.       symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
  308.       symbol_insert (symbolP, symbol_rootP, &symbol_rootP, &symbol_lastP);
  309.     }                /* if not first on the list */
  310. }
  311.  
  312. /* Line number handling */
  313.  
  314. struct line_no {
  315.   struct line_no *next;
  316.   fragS *frag;
  317.   alent l;
  318. };
  319.  
  320. int coff_line_base;
  321.  
  322. /* Symbol of last function, which we should hang line#s off of.  */
  323. static symbolS *line_fsym;
  324.  
  325. #define in_function()        (line_fsym != 0)
  326. #define clear_function()    (line_fsym = 0)
  327. #define set_function(F)        (line_fsym = (F), coff_add_linesym (F))
  328.  
  329.  
  330. void
  331. obj_symbol_new_hook (symbolP)
  332.      symbolS *symbolP;
  333. {
  334.   char underscore = 0;        /* Symbol has leading _ */
  335.  
  336.   {
  337.     long sz = (OBJ_COFF_MAX_AUXENTRIES + 1) * sizeof (combined_entry_type);
  338.     char *s = (char *) bfd_alloc_by_size_t (stdoutput, sz);
  339.     memset (s, 0, sz);
  340.     coffsymbol (symbolP->bsym)->native = (combined_entry_type *) s;
  341.   }
  342.   S_SET_DATA_TYPE (symbolP, T_NULL);
  343.   S_SET_STORAGE_CLASS (symbolP, 0);
  344.   S_SET_NUMBER_AUXILIARY (symbolP, 0);
  345.  
  346.   if (S_IS_STRING (symbolP))
  347.     SF_SET_STRING (symbolP);
  348.   if (!underscore && S_IS_LOCAL (symbolP))
  349.     SF_SET_LOCAL (symbolP);
  350. }
  351.  
  352.  
  353. /*
  354.  * Handle .ln directives.
  355.  */
  356.  
  357. static symbolS *current_lineno_sym;
  358. static struct line_no *line_nos;
  359. /* @@ Blindly assume all .ln directives will be in the .text section...  */
  360. int coff_n_line_nos;
  361.  
  362. static void
  363. add_lineno (frag, offset, num)
  364.      fragS *frag;
  365.      int offset;
  366.      int num;
  367. {
  368.   struct line_no *new_line = (struct line_no *) bfd_alloc_by_size_t (stdoutput,
  369.                                      sizeof (struct line_no));
  370.   if (!current_lineno_sym)
  371.     {
  372.       abort ();
  373.     }
  374.   new_line->next = line_nos;
  375.   new_line->frag = frag;
  376.   new_line->l.line_number = num;
  377.   new_line->l.u.offset = offset;
  378.   line_nos = new_line;
  379.   coff_n_line_nos++;
  380. }
  381.  
  382. void
  383. coff_add_linesym (sym)
  384.      symbolS *sym;
  385. {
  386.   if (line_nos)
  387.     {
  388.       coffsymbol (current_lineno_sym->bsym)->lineno = (alent *) line_nos;
  389.       coff_n_line_nos++;
  390.       line_nos = 0;
  391.     }
  392.   current_lineno_sym = sym;
  393. }
  394.  
  395. static void
  396. obj_coff_ln (appline)
  397.      int appline;
  398. {
  399.   int l;
  400.  
  401.   if (! appline && def_symbol_in_progress != NULL)
  402.     {
  403.       as_warn (".ln pseudo-op inside .def/.endef: ignored.");
  404.       demand_empty_rest_of_line ();
  405.       return;
  406.     }
  407.  
  408.   l = get_absolute_expression ();
  409.   if (!appline)
  410.     {
  411.       add_lineno (frag_now, frag_now_fix (), l);
  412.     }
  413.  
  414. #ifndef NO_LISTING
  415.   {
  416.     extern int listing;
  417.  
  418.     if (listing)
  419.       {
  420.     if (! appline)
  421.       l += coff_line_base - 1;
  422.     listing_source_line (l);
  423.       }
  424.   }
  425. #endif
  426.  
  427.   demand_empty_rest_of_line ();
  428. }
  429.  
  430. /*
  431.  *            def()
  432.  *
  433.  * Handle .def directives.
  434.  *
  435.  * One might ask : why can't we symbol_new if the symbol does not
  436.  * already exist and fill it with debug information.  Because of
  437.  * the C_EFCN special symbol. It would clobber the value of the
  438.  * function symbol before we have a chance to notice that it is
  439.  * a C_EFCN. And a second reason is that the code is more clear this
  440.  * way. (at least I think it is :-).
  441.  *
  442.  */
  443.  
  444. #define SKIP_SEMI_COLON()    while (*input_line_pointer++ != ';')
  445. #define SKIP_WHITESPACES()    while (*input_line_pointer == ' ' || \
  446.                        *input_line_pointer == '\t') \
  447.     input_line_pointer++;
  448.  
  449. static void
  450. obj_coff_def (what)
  451.      int what;
  452. {
  453.   char name_end;        /* Char after the end of name */
  454.   char *symbol_name;        /* Name of the debug symbol */
  455.   char *symbol_name_copy;    /* Temporary copy of the name */
  456.   unsigned int symbol_name_length;
  457.  
  458.   if (def_symbol_in_progress != NULL)
  459.     {
  460.       as_warn (".def pseudo-op used inside of .def/.endef: ignored.");
  461.       demand_empty_rest_of_line ();
  462.       return;
  463.     }                /* if not inside .def/.endef */
  464.  
  465.   SKIP_WHITESPACES ();
  466.  
  467.   symbol_name = input_line_pointer;
  468. #ifdef STRIP_UNDERSCORE
  469.   if (symbol_name[0] == '_' && symbol_name[1] != 0)
  470.     symbol_name++;
  471. #endif /* STRIP_UNDERSCORE */
  472.  
  473.   name_end = get_symbol_end ();
  474.   symbol_name_length = strlen (symbol_name);
  475.   symbol_name_copy = xmalloc (symbol_name_length + 1);
  476.   strcpy (symbol_name_copy, symbol_name);
  477.  
  478.   /* Initialize the new symbol */
  479.   def_symbol_in_progress = symbol_make (symbol_name_copy);
  480.   def_symbol_in_progress->sy_frag = &zero_address_frag;
  481.   S_SET_VALUE (def_symbol_in_progress, 0);
  482.  
  483.   if (S_IS_STRING (def_symbol_in_progress))
  484.     SF_SET_STRING (def_symbol_in_progress);
  485.  
  486.   *input_line_pointer = name_end;
  487.  
  488.   demand_empty_rest_of_line ();
  489. }
  490.  
  491. unsigned int dim_index;
  492.  
  493. static void
  494. obj_coff_endef (ignore)
  495.      int ignore;
  496. {
  497.   symbolS *symbolP;
  498.   /* DIM BUG FIX sac@cygnus.com */
  499.   dim_index = 0;
  500.   if (def_symbol_in_progress == NULL)
  501.     {
  502.       as_warn (".endef pseudo-op used outside of .def/.endef: ignored.");
  503.       demand_empty_rest_of_line ();
  504.       return;
  505.     }                /* if not inside .def/.endef */
  506.  
  507.   /* Set the section number according to storage class. */
  508.   switch (S_GET_STORAGE_CLASS (def_symbol_in_progress))
  509.     {
  510.     case C_STRTAG:
  511.     case C_ENTAG:
  512.     case C_UNTAG:
  513.       SF_SET_TAG (def_symbol_in_progress);
  514.       /* intentional fallthrough */
  515.     case C_FILE:
  516.     case C_TPDEF:
  517.       SF_SET_DEBUG (def_symbol_in_progress);
  518.       S_SET_SEGMENT (def_symbol_in_progress, fetch_coff_debug_section ());
  519.       break;
  520.  
  521.     case C_EFCN:
  522.       SF_SET_LOCAL (def_symbol_in_progress);    /* Do not emit this symbol. */
  523.       /* intentional fallthrough */
  524.     case C_BLOCK:
  525.       SF_SET_PROCESS (def_symbol_in_progress);    /* Will need processing before writing */
  526.       /* intentional fallthrough */
  527.     case C_FCN:
  528.       {
  529.     CONST char *name;
  530.     S_SET_SEGMENT (def_symbol_in_progress, text_section);
  531.  
  532.     name = bfd_asymbol_name (def_symbol_in_progress->bsym);
  533.     if (name[1] == 'b' && name[2] == 'f')
  534.       {
  535.         if (! in_function ())
  536.           as_warn ("`%s' symbol without preceding function", name);
  537. /*        SA_SET_SYM_LNNO (def_symbol_in_progress, 12345);*/
  538.         /* Will need relocating */
  539.         SF_SET_PROCESS (def_symbol_in_progress);
  540.         clear_function ();
  541.       }
  542.       }
  543.       break;
  544.  
  545. #ifdef C_AUTOARG
  546.     case C_AUTOARG:
  547. #endif /* C_AUTOARG */
  548.     case C_AUTO:
  549.     case C_REG:
  550.     case C_ARG:
  551.     case C_REGPARM:
  552.     case C_FIELD:
  553.       SF_SET_DEBUG (def_symbol_in_progress);
  554.       S_SET_SEGMENT (def_symbol_in_progress, absolute_section);
  555.       break;
  556.  
  557.     case C_MOS:
  558.     case C_MOE:
  559.     case C_MOU:
  560.     case C_EOS:
  561.       S_SET_SEGMENT (def_symbol_in_progress, absolute_section);
  562.       break;
  563.  
  564.     case C_EXT:
  565.     case C_STAT:
  566.     case C_LABEL:
  567.       /* Valid but set somewhere else (s_comm, s_lcomm, colon) */
  568.       break;
  569.  
  570.     case C_USTATIC:
  571.     case C_EXTDEF:
  572.     case C_ULABEL:
  573.       as_warn ("unexpected storage class %d",
  574.            S_GET_STORAGE_CLASS (def_symbol_in_progress));
  575.       break;
  576.     }                /* switch on storage class */
  577.  
  578.   /* Now that we have built a debug symbol, try to find if we should
  579.      merge with an existing symbol or not.  If a symbol is C_EFCN or
  580.      SEG_ABSOLUTE or untagged SEG_DEBUG it never merges. */
  581.  
  582.   /* Two cases for functions.  Either debug followed by definition or
  583.      definition followed by debug.  For definition first, we will
  584.      merge the debug symbol into the definition.  For debug first, the
  585.      lineno entry MUST point to the definition function or else it
  586.      will point off into space when obj_crawl_symbol_chain() merges
  587.      the debug symbol into the real symbol.  Therefor, let's presume
  588.      the debug symbol is a real function reference. */
  589.  
  590.   /* FIXME-SOON If for some reason the definition label/symbol is
  591.      never seen, this will probably leave an undefined symbol at link
  592.      time. */
  593.  
  594.   if (S_GET_STORAGE_CLASS (def_symbol_in_progress) == C_EFCN
  595.       || (!strcmp (bfd_get_section_name (stdoutput,
  596.                      S_GET_SEGMENT (def_symbol_in_progress)),
  597.            "*DEBUG*")
  598.       && !SF_GET_TAG (def_symbol_in_progress))
  599.       || S_GET_SEGMENT (def_symbol_in_progress) == absolute_section
  600.       || (symbolP = symbol_find_base (S_GET_NAME (def_symbol_in_progress), DO_NOT_STRIP)) == NULL)
  601.     {
  602.       if (def_symbol_in_progress != symbol_lastP)
  603.     symbol_append (def_symbol_in_progress, symbol_lastP, &symbol_rootP,
  604.                &symbol_lastP);
  605.     }
  606.   else
  607.     {
  608.       /* This symbol already exists, merge the newly created symbol
  609.      into the old one.  This is not mandatory. The linker can
  610.      handle duplicate symbols correctly. But I guess that it save
  611.      a *lot* of space if the assembly file defines a lot of
  612.      symbols. [loic] */
  613.  
  614.       /* The debug entry (def_symbol_in_progress) is merged into the
  615.      previous definition. */
  616.  
  617.       c_symbol_merge (def_symbol_in_progress, symbolP);
  618.       symbol_remove (def_symbol_in_progress, &symbol_rootP, &symbol_lastP);
  619.  
  620.       def_symbol_in_progress = symbolP;
  621.  
  622.       if (SF_GET_FUNCTION (def_symbol_in_progress)
  623.       || SF_GET_TAG (def_symbol_in_progress))
  624.     {
  625.       /* For functions, and tags, the symbol *must* be where the
  626.          debug symbol appears.  Move the existing symbol to the
  627.          current place. */
  628.       /* If it already is at the end of the symbol list, do nothing */
  629.       if (def_symbol_in_progress != symbol_lastP)
  630.         {
  631.           symbol_remove (def_symbol_in_progress, &symbol_rootP, &symbol_lastP);
  632.           symbol_append (def_symbol_in_progress, symbol_lastP, &symbol_rootP, &symbol_lastP);
  633.         }
  634.     }
  635.     }
  636.  
  637.   if (SF_GET_TAG (def_symbol_in_progress))
  638.     {
  639.       symbolS *oldtag;
  640.  
  641.       oldtag = symbol_find_base (S_GET_NAME (def_symbol_in_progress),
  642.                  DO_NOT_STRIP);
  643.       if (oldtag == NULL || ! SF_GET_TAG (oldtag))
  644.     tag_insert (S_GET_NAME (def_symbol_in_progress),
  645.             def_symbol_in_progress);
  646.     }
  647.  
  648.   if (SF_GET_FUNCTION (def_symbol_in_progress))
  649.     {
  650.       know (sizeof (def_symbol_in_progress) <= sizeof (long));
  651.       set_function (def_symbol_in_progress);
  652.       SF_SET_PROCESS (def_symbol_in_progress);
  653.  
  654.       if (symbolP == NULL)
  655.     {
  656.       /* That is, if this is the first time we've seen the
  657.          function... */
  658.       symbol_table_insert (def_symbol_in_progress);
  659.     } /* definition follows debug */
  660.     } /* Create the line number entry pointing to the function being defined */
  661.  
  662.   def_symbol_in_progress = NULL;
  663.   demand_empty_rest_of_line ();
  664. }
  665.  
  666. static void
  667. obj_coff_dim (ignore)
  668.      int ignore;
  669. {
  670.   int dim_index;
  671.  
  672.   if (def_symbol_in_progress == NULL)
  673.     {
  674.       as_warn (".dim pseudo-op used outside of .def/.endef: ignored.");
  675.       demand_empty_rest_of_line ();
  676.       return;
  677.     }                /* if not inside .def/.endef */
  678.  
  679.   S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
  680.  
  681.   for (dim_index = 0; dim_index < DIMNUM; dim_index++)
  682.     {
  683.       SKIP_WHITESPACES ();
  684.       SA_SET_SYM_DIMEN (def_symbol_in_progress, dim_index,
  685.             get_absolute_expression ());
  686.  
  687.       switch (*input_line_pointer)
  688.     {
  689.     case ',':
  690.       input_line_pointer++;
  691.       break;
  692.  
  693.     default:
  694.       as_warn ("badly formed .dim directive ignored");
  695.       /* intentional fallthrough */
  696.     case '\n':
  697.     case ';':
  698.       dim_index = DIMNUM;
  699.       break;
  700.     }
  701.     }
  702.  
  703.   demand_empty_rest_of_line ();
  704. }
  705.  
  706. static void
  707. obj_coff_line (ignore)
  708.      int ignore;
  709. {
  710.   int this_base;
  711.  
  712.   if (def_symbol_in_progress == NULL)
  713.     {
  714.       /* Probably stabs-style line?  */
  715.       obj_coff_ln (0);
  716.       return;
  717.     }
  718.  
  719.   this_base = get_absolute_expression ();
  720.   if (!strcmp (".bf", S_GET_NAME (def_symbol_in_progress)))
  721.     coff_line_base = this_base;
  722.  
  723.   S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
  724.   SA_SET_SYM_LNNO (def_symbol_in_progress, coff_line_base);
  725.  
  726.   demand_empty_rest_of_line ();
  727.  
  728. #ifndef NO_LISTING
  729.   if (strcmp (".bf", S_GET_NAME (def_symbol_in_progress)) == 0)
  730.     {
  731.       extern int listing;
  732.  
  733.       if (listing)
  734.     listing_source_line ((unsigned int) coff_line_base);
  735.     }
  736. #endif
  737. }
  738.  
  739. static void
  740. obj_coff_size (ignore)
  741.      int ignore;
  742. {
  743.   if (def_symbol_in_progress == NULL)
  744.     {
  745.       as_warn (".size pseudo-op used outside of .def/.endef ignored.");
  746.       demand_empty_rest_of_line ();
  747.       return;
  748.     }                /* if not inside .def/.endef */
  749.  
  750.   S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
  751.   SA_SET_SYM_SIZE (def_symbol_in_progress, get_absolute_expression ());
  752.   demand_empty_rest_of_line ();
  753. }
  754.  
  755. static void
  756. obj_coff_scl (ignore)
  757.      int ignore;
  758. {
  759.   if (def_symbol_in_progress == NULL)
  760.     {
  761.       as_warn (".scl pseudo-op used outside of .def/.endef ignored.");
  762.       demand_empty_rest_of_line ();
  763.       return;
  764.     }                /* if not inside .def/.endef */
  765.  
  766.   S_SET_STORAGE_CLASS (def_symbol_in_progress, get_absolute_expression ());
  767.   demand_empty_rest_of_line ();
  768. }
  769.  
  770. static void
  771. obj_coff_tag (ignore)
  772.      int ignore;
  773. {
  774.   char *symbol_name;
  775.   char name_end;
  776.  
  777.   if (def_symbol_in_progress == NULL)
  778.     {
  779.       as_warn (".tag pseudo-op used outside of .def/.endef ignored.");
  780.       demand_empty_rest_of_line ();
  781.       return;
  782.     }
  783.  
  784.   S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
  785.   symbol_name = input_line_pointer;
  786.   name_end = get_symbol_end ();
  787.  
  788.   /* Assume that the symbol referred to by .tag is always defined.
  789.      This was a bad assumption.  I've added find_or_make. xoxorich. */
  790.   SA_SET_SYM_TAGNDX (def_symbol_in_progress,
  791.              tag_find_or_make (symbol_name));
  792.   if (SA_GET_SYM_TAGNDX (def_symbol_in_progress) == 0L)
  793.     {
  794.       as_warn ("tag not found for .tag %s", symbol_name);
  795.     }                /* not defined */
  796.  
  797.   SF_SET_TAGGED (def_symbol_in_progress);
  798.   *input_line_pointer = name_end;
  799.  
  800.   demand_empty_rest_of_line ();
  801. }
  802.  
  803. static void
  804. obj_coff_type (ignore)
  805.      int ignore;
  806. {
  807.   if (def_symbol_in_progress == NULL)
  808.     {
  809.       as_warn (".type pseudo-op used outside of .def/.endef ignored.");
  810.       demand_empty_rest_of_line ();
  811.       return;
  812.     }                /* if not inside .def/.endef */
  813.  
  814.   S_SET_DATA_TYPE (def_symbol_in_progress, get_absolute_expression ());
  815.  
  816.   if (ISFCN (S_GET_DATA_TYPE (def_symbol_in_progress)) &&
  817.       S_GET_STORAGE_CLASS (def_symbol_in_progress) != C_TPDEF)
  818.     {
  819.       SF_SET_FUNCTION (def_symbol_in_progress);
  820.     }                /* is a function */
  821.  
  822.   demand_empty_rest_of_line ();
  823. }
  824.  
  825. static void
  826. obj_coff_val (ignore)
  827.      int ignore;
  828. {
  829.   if (def_symbol_in_progress == NULL)
  830.     {
  831.       as_warn (".val pseudo-op used outside of .def/.endef ignored.");
  832.       demand_empty_rest_of_line ();
  833.       return;
  834.     }                /* if not inside .def/.endef */
  835.  
  836.   if (is_name_beginner (*input_line_pointer))
  837.     {
  838.       char *symbol_name = input_line_pointer;
  839.       char name_end = get_symbol_end ();
  840.  
  841.       if (!strcmp (symbol_name, "."))
  842.     {
  843.       def_symbol_in_progress->sy_frag = frag_now;
  844.       S_SET_VALUE (def_symbol_in_progress, (valueT) frag_now_fix ());
  845.       /* If the .val is != from the .def (e.g. statics) */
  846.     }
  847.       else if (strcmp (S_GET_NAME (def_symbol_in_progress), symbol_name))
  848.     {
  849.       def_symbol_in_progress->sy_value.X_op = O_symbol;
  850.       def_symbol_in_progress->sy_value.X_add_symbol =
  851.         symbol_find_or_make (symbol_name);
  852.       def_symbol_in_progress->sy_value.X_op_symbol = NULL;
  853.       def_symbol_in_progress->sy_value.X_add_number = 0;
  854.  
  855.       /* If the segment is undefined when the forward reference is
  856.          resolved, then copy the segment id from the forward
  857.          symbol.  */
  858.       SF_SET_GET_SEGMENT (def_symbol_in_progress);
  859.     }
  860.       /* Otherwise, it is the name of a non debug symbol and its value will be calculated later. */
  861.       *input_line_pointer = name_end;
  862.     }
  863.   else
  864.     {
  865.       S_SET_VALUE (def_symbol_in_progress, get_absolute_expression ());
  866.     }                /* if symbol based */
  867.  
  868.   demand_empty_rest_of_line ();
  869. }
  870.  
  871. void
  872. obj_read_begin_hook ()
  873. {
  874.   /* These had better be the same.  Usually 18 bytes. */
  875. #ifndef BFD_HEADERS
  876.   know (sizeof (SYMENT) == sizeof (AUXENT));
  877.   know (SYMESZ == AUXESZ);
  878. #endif
  879.   tag_init ();
  880. }
  881.  
  882.  
  883. symbolS *coff_last_function;
  884. static symbolS *coff_last_bf;
  885.  
  886. void
  887. coff_frob_symbol (symp, punt)
  888.      symbolS *symp;
  889.      int *punt;
  890. {
  891.   static symbolS *last_tagP;
  892.   static stack *block_stack;
  893.   static symbolS *set_end;
  894.   symbolS *next_set_end = NULL;
  895.  
  896.   if (symp == &abs_symbol)
  897.     {
  898.       *punt = 1;
  899.       return;
  900.     }
  901.  
  902.   if (current_lineno_sym)
  903.     coff_add_linesym ((symbolS *) 0);
  904.  
  905.   if (!block_stack)
  906.     block_stack = stack_init (512, sizeof (symbolS*));
  907.  
  908.   if (!S_IS_DEFINED (symp) && S_GET_STORAGE_CLASS (symp) != C_STAT)
  909.     S_SET_STORAGE_CLASS (symp, C_EXT);
  910.  
  911.   if (!SF_GET_DEBUG (symp))
  912.     {
  913.       symbolS *real;
  914.       if (!SF_GET_LOCAL (symp)
  915.       && !SF_GET_STATICS (symp)
  916.       && (real = symbol_find_base (S_GET_NAME (symp), DO_NOT_STRIP))
  917.       && real != symp)
  918.     {
  919.       c_symbol_merge (symp, real);
  920.       *punt = 1;
  921.     }
  922.       if (!S_IS_DEFINED (symp) && !SF_GET_LOCAL (symp))
  923.     {
  924.       assert (S_GET_VALUE (symp) == 0);
  925.       S_SET_EXTERNAL (symp);
  926.     }
  927.       else if (S_GET_STORAGE_CLASS (symp) == C_NULL)
  928.     {
  929.       if (S_GET_SEGMENT (symp) == text_section
  930.           && symp != seg_info (text_section)->sym)
  931.         S_SET_STORAGE_CLASS (symp, C_LABEL);
  932.       else
  933.         S_SET_STORAGE_CLASS (symp, C_STAT);
  934.     }
  935.       if (SF_GET_PROCESS (symp))
  936.     {
  937.       if (S_GET_STORAGE_CLASS (symp) == C_BLOCK)
  938.         {
  939.           if (!strcmp (S_GET_NAME (symp), ".bb"))
  940.         stack_push (block_stack, (char *) &symp);
  941.           else
  942.         {
  943.           symbolS *begin;
  944.           begin = *(symbolS **) stack_pop (block_stack);
  945.           if (begin == 0)
  946.             as_warn ("mismatched .eb");
  947.           else
  948.             next_set_end = begin;
  949.         }
  950.         }
  951.       if (coff_last_function == 0 && SF_GET_FUNCTION (symp))
  952.         {
  953.           union internal_auxent *auxp;
  954.           coff_last_function = symp;
  955.           if (S_GET_NUMBER_AUXILIARY (symp) < 1)
  956.         S_SET_NUMBER_AUXILIARY (symp, 1);
  957.           auxp = &coffsymbol (symp->bsym)->native[1].u.auxent;
  958.           memset (auxp->x_sym.x_fcnary.x_ary.x_dimen, 0,
  959.               sizeof (auxp->x_sym.x_fcnary.x_ary.x_dimen));
  960.         }
  961.       if (S_GET_STORAGE_CLASS (symp) == C_EFCN)
  962.         {
  963.           if (coff_last_function == 0)
  964.         as_fatal ("C_EFCN symbol out of scope");
  965.           SA_SET_SYM_FSIZE (coff_last_function,
  966.                 (long) (S_GET_VALUE (symp)
  967.                     - S_GET_VALUE (coff_last_function)));
  968.           next_set_end = coff_last_function;
  969.           coff_last_function = 0;
  970.         }
  971.     }
  972.       if (S_IS_EXTERNAL (symp))
  973.     S_SET_STORAGE_CLASS (symp, C_EXT);
  974.       else if (SF_GET_LOCAL (symp))
  975.     *punt = 1;
  976.  
  977.       if (SF_GET_FUNCTION (symp))
  978.     symp->bsym->flags |= BSF_FUNCTION;
  979.  
  980.       /* more ... */
  981.     }
  982.  
  983.   if (SF_GET_TAG (symp))
  984.     last_tagP = symp;
  985.   else if (S_GET_STORAGE_CLASS (symp) == C_EOS)
  986.     next_set_end = last_tagP;
  987.  
  988. #ifdef OBJ_XCOFF
  989.   /* This is pretty horrible, but we have to set *punt correctly in
  990.      order to call SA_SET_SYM_ENDNDX correctly.  */
  991.   if (! symp->sy_used_in_reloc
  992.       && ((symp->bsym->flags & BSF_SECTION_SYM) != 0
  993.       || (! S_IS_EXTERNAL (symp)
  994.           && ! symp->sy_tc.output
  995.           && S_GET_STORAGE_CLASS (symp) != C_FILE)))
  996.     *punt = 1;
  997. #endif
  998.  
  999.   if (set_end != (symbolS *) NULL
  1000.       && ! *punt
  1001.       && ((symp->bsym->flags & BSF_NOT_AT_END) != 0
  1002.       || (S_IS_DEFINED (symp)
  1003.           && ! S_IS_COMMON (symp)
  1004.           && (! S_IS_EXTERNAL (symp) || SF_GET_FUNCTION (symp)))))
  1005.     {
  1006.       SA_SET_SYM_ENDNDX (set_end, symp);
  1007.       set_end = NULL;
  1008.     }
  1009.  
  1010.   if (next_set_end != NULL
  1011.       && ! *punt)
  1012.     set_end = next_set_end;
  1013.  
  1014.   if (! *punt
  1015.       && S_GET_STORAGE_CLASS (symp) == C_FCN
  1016.       && strcmp (S_GET_NAME (symp), ".bf") == 0)
  1017.     {
  1018.       if (coff_last_bf != NULL)
  1019.     SA_SET_SYM_ENDNDX (coff_last_bf, symp);
  1020.       coff_last_bf = symp;
  1021.     }
  1022.  
  1023.   if (coffsymbol (symp->bsym)->lineno)
  1024.     {
  1025.       int i;
  1026.       struct line_no *lptr;
  1027.       alent *l;
  1028.  
  1029.       lptr = (struct line_no *) coffsymbol (symp->bsym)->lineno;
  1030.       for (i = 0; lptr; lptr = lptr->next)
  1031.     i++;
  1032.       lptr = (struct line_no *) coffsymbol (symp->bsym)->lineno;
  1033.  
  1034.       /* We need i entries for line numbers, plus 1 for the first
  1035.      entry which BFD will override, plus 1 for the last zero
  1036.      entry (a marker for BFD).  */
  1037.       l = (alent *) bfd_alloc_by_size_t (stdoutput, (i + 2) * sizeof (alent));
  1038.       coffsymbol (symp->bsym)->lineno = l;
  1039.       l[i + 1].line_number = 0;
  1040.       l[i + 1].u.sym = NULL;
  1041.       for (; i > 0; i--)
  1042.     {
  1043.       if (lptr->frag)
  1044.         lptr->l.u.offset += lptr->frag->fr_address;
  1045.       l[i] = lptr->l;
  1046.       lptr = lptr->next;
  1047.     }
  1048.     }
  1049. }
  1050.  
  1051. void
  1052. coff_adjust_section_syms (abfd, sec, x)
  1053.      bfd *abfd;
  1054.      asection *sec;
  1055.      PTR x;
  1056. {
  1057.   symbolS *secsym;
  1058.   segment_info_type *seginfo = seg_info (sec);
  1059.   int nlnno, nrelocs = 0;
  1060.  
  1061.   /* RS/6000 gas creates a .debug section manually in ppc_frob_file in
  1062.      tc-ppc.c.  Do not get confused by it.  */
  1063.   if (seginfo == NULL)
  1064.     return;
  1065.  
  1066.   if (!strcmp (sec->name, ".text"))
  1067.     nlnno = coff_n_line_nos;
  1068.   else
  1069.     nlnno = 0;
  1070.   {
  1071.     /* @@ Hope that none of the fixups expand to more than one reloc
  1072.        entry...  */
  1073.     fixS *fixp = seginfo->fix_root;
  1074.     while (fixp)
  1075.       {
  1076.     fixp = fixp->fx_next;
  1077.     nrelocs++;
  1078.       }
  1079.   }
  1080.   if (bfd_get_section_size_before_reloc (sec) == 0
  1081.       && nrelocs == 0
  1082.       && nlnno == 0
  1083.       && sec != text_section
  1084.       && sec != data_section
  1085.       && sec != bss_section)
  1086.     return;
  1087.   secsym = section_symbol (sec);
  1088.   SA_SET_SCN_NRELOC (secsym, nrelocs);
  1089.   SA_SET_SCN_NLINNO (secsym, nlnno);
  1090. }
  1091.  
  1092. void
  1093. coff_frob_file ()
  1094. {
  1095.   bfd_map_over_sections (stdoutput, coff_adjust_section_syms, (char*) 0);
  1096. }
  1097.  
  1098. /*
  1099.  * implement the .section pseudo op:
  1100.  *    .section name {, "flags"}
  1101.  *                ^         ^
  1102.  *                |         +--- optional flags: 'b' for bss
  1103.  *                |                              'i' for info
  1104.  *                +-- section name               'l' for lib
  1105.  *                                               'n' for noload
  1106.  *                                               'o' for over
  1107.  *                                               'w' for data
  1108.  *                         'd' (apparently m88k for data)
  1109.  *                                               'x' for text
  1110.  * But if the argument is not a quoted string, treat it as a
  1111.  * subsegment number.
  1112.  */
  1113.  
  1114. void
  1115. obj_coff_section (ignore)
  1116.      int ignore;
  1117. {
  1118.   /* Strip out the section name */
  1119.   char *section_name;
  1120.   char c;
  1121.   char *name;
  1122.   unsigned int exp;
  1123.   flagword flags;
  1124.   asection *sec;
  1125.  
  1126.   if (flag_mri)
  1127.     {
  1128.       char type;
  1129.  
  1130.       s_mri_sect (&type);
  1131.       return;
  1132.     }
  1133.  
  1134.   section_name = input_line_pointer;
  1135.   c = get_symbol_end ();
  1136.  
  1137.   name = xmalloc (input_line_pointer - section_name + 1);
  1138.   strcpy (name, section_name);
  1139.  
  1140.   *input_line_pointer = c;
  1141.  
  1142.   SKIP_WHITESPACE ();
  1143.  
  1144.   exp = 0;
  1145.   flags = SEC_NO_FLAGS;
  1146.  
  1147.   if (*input_line_pointer == ',')
  1148.     {
  1149.       ++input_line_pointer;
  1150.       SKIP_WHITESPACE ();
  1151.       if (*input_line_pointer != '"')
  1152.     exp = get_absolute_expression ();
  1153.       else
  1154.     {
  1155.       ++input_line_pointer;
  1156.       while (*input_line_pointer != '"'
  1157.          && ! is_end_of_line[(unsigned char) *input_line_pointer])
  1158.         {
  1159.           switch (*input_line_pointer)
  1160.         {
  1161.         case 'b': flags |= SEC_ALLOC; flags &=~ SEC_LOAD; break;
  1162.         case 'n': flags &=~ SEC_LOAD; break;
  1163.         case 'd':
  1164.         case 'w': flags &=~ SEC_READONLY; break;
  1165.         case 'x': flags |= SEC_CODE; break;
  1166.  
  1167.         case 'i': /* STYP_INFO */
  1168.         case 'l': /* STYP_LIB */
  1169.         case 'o': /* STYP_OVER */
  1170.           as_warn ("unsupported section attribute '%c'",
  1171.                *input_line_pointer);
  1172.           break;
  1173.  
  1174.         default:
  1175.           as_warn("unknown section attribute '%c'",
  1176.               *input_line_pointer);
  1177.           break;
  1178.         }
  1179.           ++input_line_pointer;
  1180.         }
  1181.       if (*input_line_pointer == '"')
  1182.         ++input_line_pointer;
  1183.     }
  1184.     }
  1185.  
  1186.   sec = subseg_new (name, (subsegT) exp);
  1187.  
  1188.   if (flags != SEC_NO_FLAGS)
  1189.     {
  1190.       if (! bfd_set_section_flags (stdoutput, sec, flags))
  1191.     as_warn ("error setting flags for \"%s\": %s",
  1192.          bfd_section_name (stdoutput, sec),
  1193.          bfd_errmsg (bfd_get_error ()));
  1194.     }
  1195.  
  1196.   demand_empty_rest_of_line ();
  1197. }
  1198.  
  1199. void
  1200. coff_adjust_symtab ()
  1201. {
  1202.   if (symbol_rootP == NULL
  1203.       || S_GET_STORAGE_CLASS (symbol_rootP) != C_FILE)
  1204.     c_dot_file_symbol ("fake");
  1205. }
  1206.  
  1207. void
  1208. coff_frob_section (sec)
  1209.      segT sec;
  1210. {
  1211.   segT strsec;
  1212.   char *p;
  1213.   fragS *fragp;
  1214.   bfd_vma size, n_entries, mask;
  1215.  
  1216.   /* The COFF back end in BFD requires that all section sizes be
  1217.      rounded up to multiples of the corresponding section alignments.
  1218.      Seems kinda silly to me, but that's the way it is.  */
  1219.   size = bfd_get_section_size_before_reloc (sec);
  1220.   mask = ((bfd_vma) 1 << (bfd_vma) sec->alignment_power) - 1;
  1221.   if (size & mask)
  1222.     {
  1223.       size = (size + mask) & ~mask;
  1224.       bfd_set_section_size (stdoutput, sec, size);
  1225.     }
  1226.  
  1227.   /* If the section size is non-zero, the section symbol needs an aux
  1228.      entry associated with it, indicating the size.  We don't know
  1229.      all the values yet; coff_frob_symbol will fill them in later.  */
  1230.   if (size != 0
  1231.       || sec == text_section
  1232.       || sec == data_section
  1233.       || sec == bss_section)
  1234.     {
  1235.       symbolS *secsym = section_symbol (sec);
  1236.  
  1237.       S_SET_STORAGE_CLASS (secsym, C_STAT);
  1238.       S_SET_NUMBER_AUXILIARY (secsym, 1);
  1239.       SF_SET_STATICS (secsym);
  1240.       SA_SET_SCN_SCNLEN (secsym, size);
  1241.     }
  1242.  
  1243.   /* @@ these should be in a "stabs.h" file, or maybe as.h */
  1244. #ifndef STAB_SECTION_NAME
  1245. #define STAB_SECTION_NAME ".stab"
  1246. #endif
  1247. #ifndef STAB_STRING_SECTION_NAME
  1248. #define STAB_STRING_SECTION_NAME ".stabstr"
  1249. #endif
  1250.   if (strcmp (STAB_STRING_SECTION_NAME, sec->name))
  1251.     return;
  1252.  
  1253.   strsec = sec;
  1254.   sec = subseg_get (STAB_SECTION_NAME, 0);
  1255.   /* size is already rounded up, since other section will be listed first */
  1256.   size = bfd_get_section_size_before_reloc (strsec);
  1257.  
  1258.   n_entries = bfd_get_section_size_before_reloc (sec) / 12 - 1;
  1259.  
  1260.   /* Find first non-empty frag.  It should be large enough.  */
  1261.   fragp = seg_info (sec)->frchainP->frch_root;
  1262.   while (fragp && fragp->fr_fix == 0)
  1263.     fragp = fragp->fr_next;
  1264.   assert (fragp != 0 && fragp->fr_fix >= 12);
  1265.  
  1266.   /* Store the values.  */
  1267.   p = fragp->fr_literal;
  1268.   bfd_h_put_16 (stdoutput, n_entries, (bfd_byte *) p + 6);
  1269.   bfd_h_put_32 (stdoutput, size, (bfd_byte *) p + 8);
  1270. }
  1271.  
  1272. void
  1273. obj_coff_init_stab_section (seg)
  1274.      segT seg;
  1275. {
  1276.   char *file;
  1277.   char *p;
  1278.   char *stabstr_name;
  1279.   unsigned int stroff;
  1280.  
  1281.   /* Make space for this first symbol. */
  1282.   p = frag_more (12);
  1283.   /* Zero it out. */
  1284.   memset (p, 0, 12);
  1285.   as_where (&file, (unsigned int *) NULL);
  1286.   stabstr_name = (char *) alloca (strlen (seg->name) + 4);
  1287.   strcpy (stabstr_name, seg->name);
  1288.   strcat (stabstr_name, "str");
  1289.   stroff = get_stab_string_offset (file, stabstr_name);
  1290.   know (stroff == 1);
  1291.   md_number_to_chars (p, stroff, 4);
  1292. }
  1293.  
  1294. #ifdef DEBUG
  1295. /* for debugging */
  1296. const char *
  1297. s_get_name (s)
  1298.      symbolS *s;
  1299. {
  1300.   return ((s == NULL) ? "(NULL)" : S_GET_NAME (s));
  1301. }
  1302.  
  1303. void
  1304. symbol_dump ()
  1305. {
  1306.   symbolS *symbolP;
  1307.  
  1308.   for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
  1309.     {
  1310.       printf("0x%lx: \"%s\" type = %ld, class = %d, segment = %d\n",
  1311.          (unsigned long) symbolP,
  1312.          S_GET_NAME(symbolP),
  1313.          (long) S_GET_DATA_TYPE(symbolP),
  1314.          S_GET_STORAGE_CLASS(symbolP),
  1315.          (int) S_GET_SEGMENT(symbolP));
  1316.     }
  1317. }
  1318.  
  1319. #endif /* DEBUG */
  1320.  
  1321. #else /* not BFD_ASSEMBLER */
  1322.  
  1323. #include "frags.h"
  1324. /* This is needed because we include internal bfd things. */
  1325. #include <time.h>
  1326.  
  1327. #include "libbfd.h"
  1328. #include "libcoff.h"
  1329.  
  1330. #ifdef TE_PE
  1331. #include "coff/pe.h"
  1332. #endif
  1333.  
  1334. /* The NOP_OPCODE is for the alignment fill value.  Fill with nop so
  1335.    that we can stick sections together without causing trouble.  */
  1336. #ifndef NOP_OPCODE
  1337. #define NOP_OPCODE 0x00
  1338. #endif
  1339.  
  1340. /* The zeroes if symbol name is longer than 8 chars */
  1341. #define S_SET_ZEROES(s,v)        ((s)->sy_symbol.ost_entry.n_zeroes = (v))
  1342.  
  1343. #define MIN(a,b) ((a) < (b)? (a) : (b))
  1344. /* This vector is used to turn an internal segment into a section #
  1345.    suitable for insertion into a coff symbol table
  1346.  */
  1347.  
  1348. const short seg_N_TYPE[] =
  1349. {                /* in: segT   out: N_TYPE bits */
  1350.   C_ABS_SECTION,
  1351.   1,    2,  3,   4,    5,   6,   7,   8,   9,  10,
  1352.   11,  12,  13,  14,  15,  16,  17,  18,  19,  20,
  1353.   21,  22,  23,  24,  25,  26,  27,  28,  29,  30,
  1354.   31,  32,  33,  34,  35,  36,  37,  38,  39,  40,
  1355.   C_UNDEF_SECTION,        /* SEG_UNKNOWN */
  1356.   C_UNDEF_SECTION,        /* SEG_GOOF */
  1357.   C_UNDEF_SECTION,        /* SEG_EXPR */
  1358.   C_DEBUG_SECTION,        /* SEG_DEBUG */
  1359.   C_NTV_SECTION,        /* SEG_NTV */
  1360.   C_PTV_SECTION,        /* SEG_PTV */
  1361.   C_REGISTER_SECTION,        /* SEG_REGISTER */
  1362. };
  1363.  
  1364. int function_lineoff = -1;    /* Offset in line#s where the last function
  1365.                    started (the odd entry for line #0) */
  1366.  
  1367. /* structure used to keep the filenames which
  1368.    are too long around so that we can stick them
  1369.    into the string table */
  1370. struct filename_list 
  1371. {
  1372.   char *filename;
  1373.   struct filename_list *next;
  1374. };
  1375.  
  1376. static struct filename_list *filename_list_head;
  1377. static struct filename_list *filename_list_tail;
  1378.  
  1379. static symbolS *last_line_symbol;
  1380.  
  1381. /* Add 4 to the real value to get the index and compensate the
  1382.    negatives. This vector is used by S_GET_SEGMENT to turn a coff
  1383.    section number into a segment number
  1384. */
  1385. static symbolS *previous_file_symbol;
  1386. void c_symbol_merge ();
  1387. static int line_base;
  1388.  
  1389. symbolS *c_section_symbol ();
  1390. bfd *abfd;
  1391.  
  1392. static void fixup_segment PARAMS ((segment_info_type *segP,
  1393.                    segT this_segment_type));
  1394.  
  1395.  
  1396. static void fixup_mdeps PARAMS ((fragS *,
  1397.                  object_headers *,
  1398.                  segT));
  1399.  
  1400.  
  1401. static void fill_section PARAMS ((bfd * abfd,
  1402.                   object_headers *,
  1403.                   unsigned long *));
  1404.  
  1405.  
  1406. static int c_line_new PARAMS ((symbolS * symbol, long paddr,
  1407.                    int line_number,
  1408.                    fragS * frag));
  1409.  
  1410.  
  1411. static void w_symbols PARAMS ((bfd * abfd, char *where,
  1412.                    symbolS * symbol_rootP));
  1413.  
  1414. static void adjust_stab_section PARAMS ((bfd *abfd, segT seg));
  1415.  
  1416. static void obj_coff_lcomm PARAMS ((int));
  1417. static void obj_coff_text PARAMS ((int));
  1418. static void obj_coff_data PARAMS ((int));
  1419. static void obj_coff_bss PARAMS ((int));
  1420. static void obj_coff_ident PARAMS ((int));
  1421. void obj_coff_section PARAMS ((int));
  1422.  
  1423. /* Section stuff
  1424.  
  1425.    We allow more than just the standard 3 sections, infact, we allow
  1426.    40 sections, (though the usual three have to be there).
  1427.  
  1428.    This structure performs the mappings for us:
  1429. */
  1430.  
  1431.  
  1432. typedef struct
  1433. {
  1434.   segT seg_t;
  1435.   int i;
  1436. } seg_info_type;
  1437.  
  1438. static const seg_info_type seg_info_off_by_4[] =
  1439. {
  1440.  {SEG_PTV,  },
  1441.  {SEG_NTV,  },
  1442.  {SEG_DEBUG, },
  1443.  {SEG_ABSOLUTE,  },
  1444.  {SEG_UNKNOWN,     },
  1445.  {SEG_E0}, {SEG_E1}, {SEG_E2}, {SEG_E3}, {SEG_E4},
  1446.  {SEG_E5}, {SEG_E6}, {SEG_E7}, {SEG_E8}, {SEG_E9},
  1447.  {SEG_E10},{SEG_E11},{SEG_E12},{SEG_E13},{SEG_E14},
  1448.  {SEG_E15},{SEG_E16},{SEG_E17},{SEG_E18},{SEG_E19},
  1449.  {SEG_E20},{SEG_E21},{SEG_E22},{SEG_E23},{SEG_E24},
  1450.  {SEG_E25},{SEG_E26},{SEG_E27},{SEG_E28},{SEG_E29},
  1451.  {SEG_E30},{SEG_E31},{SEG_E32},{SEG_E33},{SEG_E34},
  1452.  {SEG_E35},{SEG_E36},{SEG_E37},{SEG_E38},{SEG_E39},
  1453.  {(segT)40},
  1454.  {(segT)41},
  1455.  {(segT)42},
  1456.  {(segT)43},
  1457.  {(segT)44},
  1458.  {(segT)45},
  1459.  {(segT)0},
  1460.  {(segT)0},
  1461.  {(segT)0},
  1462.  {SEG_REGISTER}
  1463. };
  1464.  
  1465.  
  1466.  
  1467. #define SEG_INFO_FROM_SECTION_NUMBER(x) (seg_info_off_by_4[(x)+4])
  1468.  
  1469. static relax_addressT
  1470. relax_align (address, alignment)
  1471.      relax_addressT address;
  1472.      long alignment;
  1473. {
  1474.   relax_addressT mask;
  1475.   relax_addressT new_address;
  1476.  
  1477.   mask = ~((~0) << alignment);
  1478.   new_address = (address + mask) & (~mask);
  1479.   return (new_address - address);
  1480. }
  1481.  
  1482.  
  1483. segT
  1484. s_get_segment (x)
  1485.      symbolS * x;
  1486. {
  1487.   return SEG_INFO_FROM_SECTION_NUMBER (x->sy_symbol.ost_entry.n_scnum).seg_t;
  1488. }
  1489.  
  1490.  
  1491.  
  1492. /* calculate the size of the frag chain and fill in the section header
  1493.    to contain all of it, also fill in the addr of the sections */
  1494. static unsigned int
  1495. size_section (abfd, idx)
  1496.      bfd * abfd;
  1497.      unsigned int idx;
  1498. {
  1499.  
  1500.   unsigned int size = 0;
  1501.   fragS *frag = segment_info[idx].frchainP->frch_root;
  1502.   while (frag)
  1503.     {
  1504.       size = frag->fr_address;
  1505.       if (frag->fr_address != size)
  1506.     {
  1507.       fprintf (stderr, "Out of step\n");
  1508.       size = frag->fr_address;
  1509.     }
  1510.  
  1511.       switch (frag->fr_type)
  1512.     {
  1513. #ifdef TC_COFF_SIZEMACHDEP
  1514.     case rs_machine_dependent:
  1515.       size += TC_COFF_SIZEMACHDEP (frag);
  1516.       break;
  1517. #endif
  1518.     case rs_space:
  1519.       assert (frag->fr_symbol == 0);
  1520.     case rs_fill:
  1521.     case rs_org:
  1522.       size += frag->fr_fix;
  1523.       size += frag->fr_offset * frag->fr_var;
  1524.       break;
  1525.     case rs_align:
  1526.     case rs_align_code:
  1527.       size += frag->fr_fix;
  1528.       size += relax_align (size, frag->fr_offset);
  1529.       break;
  1530.     default:
  1531.       BAD_CASE (frag->fr_type);
  1532.       break;
  1533.     }
  1534.       frag = frag->fr_next;
  1535.     }
  1536.   segment_info[idx].scnhdr.s_size = size;
  1537.   return size;
  1538. }
  1539.  
  1540.  
  1541. static unsigned int
  1542. count_entries_in_chain (idx)
  1543.      unsigned int idx;
  1544. {
  1545.   unsigned int nrelocs;
  1546.   fixS *fixup_ptr;
  1547.  
  1548.   /* Count the relocations */
  1549.   fixup_ptr = segment_info[idx].fix_root;
  1550.   nrelocs = 0;
  1551.   while (fixup_ptr != (fixS *) NULL)
  1552.     {
  1553.       if (fixup_ptr->fx_done == 0 && TC_COUNT_RELOC (fixup_ptr))
  1554.     {
  1555. #ifdef TC_A29K
  1556.       if (fixup_ptr->fx_r_type == RELOC_CONSTH)
  1557.         nrelocs += 2;
  1558.       else
  1559.         nrelocs++;
  1560. #else
  1561.       nrelocs++;
  1562. #endif
  1563.     }
  1564.  
  1565.       fixup_ptr = fixup_ptr->fx_next;
  1566.     }
  1567.   return nrelocs;
  1568. }
  1569.  
  1570. #ifdef TE_AUX
  1571.  
  1572. static int compare_external_relocs PARAMS ((const PTR, const PTR));
  1573.  
  1574. /* AUX's ld expects relocations to be sorted */
  1575. static int
  1576. compare_external_relocs (x, y)
  1577.      const PTR x;
  1578.      const PTR y;
  1579. {
  1580.   struct external_reloc *a = (struct external_reloc *) x;
  1581.   struct external_reloc *b = (struct external_reloc *) y;
  1582.   bfd_vma aadr = bfd_getb32 (a->r_vaddr);
  1583.   bfd_vma badr = bfd_getb32 (b->r_vaddr);
  1584.   return (aadr < badr ? -1 : badr < aadr ? 1 : 0);
  1585. }
  1586.  
  1587. #endif
  1588.  
  1589. /* output all the relocations for a section */
  1590. void
  1591. do_relocs_for (abfd, h, file_cursor)
  1592.      bfd * abfd;
  1593.      object_headers * h;
  1594.      unsigned long *file_cursor;
  1595. {
  1596.   unsigned int nrelocs;
  1597.   unsigned int idx;
  1598.   unsigned long reloc_start = *file_cursor;
  1599.  
  1600.   for (idx = SEG_E0; idx < SEG_LAST; idx++)
  1601.     {
  1602.       if (segment_info[idx].scnhdr.s_name[0])
  1603.     {
  1604.       struct external_reloc *ext_ptr;
  1605.       struct external_reloc *external_reloc_vec;
  1606.       unsigned int external_reloc_size;
  1607.       unsigned int base = segment_info[idx].scnhdr.s_paddr;
  1608.       fixS *fix_ptr = segment_info[idx].fix_root;
  1609.       nrelocs = count_entries_in_chain (idx);
  1610.  
  1611.       if (nrelocs)
  1612.         /* Bypass this stuff if no relocs.  This also incidentally
  1613.            avoids a SCO bug, where free(malloc(0)) tends to crash.  */
  1614.         {
  1615.           external_reloc_size = nrelocs * RELSZ;
  1616.           external_reloc_vec =
  1617.         (struct external_reloc *) malloc (external_reloc_size);
  1618.  
  1619.           ext_ptr = external_reloc_vec;
  1620.  
  1621.           /* Fill in the internal coff style reloc struct from the
  1622.          internal fix list.  */
  1623.           while (fix_ptr)
  1624.         {
  1625.           struct internal_reloc intr;
  1626.  
  1627.           /* Only output some of the relocations */
  1628.           if (fix_ptr->fx_done == 0 && TC_COUNT_RELOC (fix_ptr))
  1629.             {
  1630. #ifdef TC_RELOC_MANGLE
  1631.               TC_RELOC_MANGLE (&segment_info[idx], fix_ptr, &intr,
  1632.                        base);
  1633.  
  1634. #else
  1635.               symbolS *dot;
  1636.               symbolS *symbol_ptr = fix_ptr->fx_addsy;
  1637.  
  1638.               intr.r_type = TC_COFF_FIX2RTYPE (fix_ptr);
  1639.               intr.r_vaddr =
  1640.             base + fix_ptr->fx_frag->fr_address + fix_ptr->fx_where;
  1641.  
  1642. #ifdef TC_KEEP_FX_OFFSET
  1643.               intr.r_offset = fix_ptr->fx_offset;
  1644. #else
  1645.               intr.r_offset = 0;
  1646. #endif
  1647.  
  1648.               while (symbol_ptr->sy_value.X_op == O_symbol
  1649.                  && (! S_IS_DEFINED (symbol_ptr)
  1650.                  || S_IS_COMMON (symbol_ptr)))
  1651.             symbol_ptr = symbol_ptr->sy_value.X_add_symbol;
  1652.  
  1653.               /* Turn the segment of the symbol into an offset.  */
  1654.               if (symbol_ptr)
  1655.             {
  1656.               if (! symbol_ptr->sy_resolved)
  1657.                 {
  1658.                   char *file;
  1659.                   unsigned int line;
  1660.  
  1661.                   if (expr_symbol_where (symbol_ptr, &file, &line))
  1662.                 as_bad_where (file, line,
  1663.                           "unresolved relocation");
  1664.                   else
  1665.                 as_bad ("bad relocation: symbol `%s' not in symbol table",
  1666.                     S_GET_NAME (symbol_ptr));
  1667.                 }
  1668.               dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
  1669.               if (dot)
  1670.                 {
  1671.                   intr.r_symndx = dot->sy_number;
  1672.                 }
  1673.               else
  1674.                 {
  1675.                   intr.r_symndx = symbol_ptr->sy_number;
  1676.                 }
  1677.  
  1678.             }
  1679.               else
  1680.             {
  1681.               intr.r_symndx = -1;
  1682.             }
  1683. #endif
  1684.  
  1685.               (void) bfd_coff_swap_reloc_out (abfd, &intr, ext_ptr);
  1686.               ext_ptr++;
  1687.  
  1688. #if defined(TC_A29K)
  1689.  
  1690.               /* The 29k has a special kludge for the high 16 bit
  1691.              reloc.  Two relocations are emited, R_IHIHALF,
  1692.              and R_IHCONST. The second one doesn't contain a
  1693.              symbol, but uses the value for offset.  */
  1694.  
  1695.               if (intr.r_type == R_IHIHALF)
  1696.             {
  1697.               /* now emit the second bit */
  1698.               intr.r_type = R_IHCONST;
  1699.               intr.r_symndx = fix_ptr->fx_addnumber;
  1700.               (void) bfd_coff_swap_reloc_out (abfd, &intr, ext_ptr);
  1701.               ext_ptr++;
  1702.             }
  1703. #endif
  1704.             }
  1705.  
  1706.           fix_ptr = fix_ptr->fx_next;
  1707.         }
  1708.  
  1709. #ifdef TE_AUX
  1710.           /* Sort the reloc table */
  1711.           qsort ((PTR) external_reloc_vec, nrelocs,
  1712.              sizeof (struct external_reloc), compare_external_relocs);
  1713. #endif
  1714.  
  1715.           /* Write out the reloc table */
  1716.           bfd_write ((PTR) external_reloc_vec, 1, external_reloc_size,
  1717.              abfd);
  1718.           free (external_reloc_vec);
  1719.  
  1720.           /* Fill in section header info.  */
  1721.           segment_info[idx].scnhdr.s_relptr = *file_cursor;
  1722.           *file_cursor += external_reloc_size;
  1723.           segment_info[idx].scnhdr.s_nreloc = nrelocs;
  1724.         }
  1725.       else
  1726.         {
  1727.           /* No relocs */
  1728.           segment_info[idx].scnhdr.s_relptr = 0;
  1729.         }
  1730.     }
  1731.     }
  1732.   /* Set relocation_size field in file headers */
  1733.   H_SET_RELOCATION_SIZE (h, *file_cursor - reloc_start, 0);
  1734. }
  1735.  
  1736.  
  1737. /* run through a frag chain and write out the data to go with it, fill
  1738.    in the scnhdrs with the info on the file postions
  1739. */
  1740. static void
  1741. fill_section (abfd, h, file_cursor)
  1742.      bfd * abfd;
  1743.      object_headers *h;
  1744.      unsigned long *file_cursor;
  1745. {
  1746.  
  1747.   unsigned int i;
  1748.   unsigned int paddr = 0;
  1749.  
  1750.   for (i = SEG_E0; i < SEG_UNKNOWN; i++)
  1751.     {
  1752.       unsigned int offset = 0;
  1753.       struct internal_scnhdr *s = &(segment_info[i].scnhdr);
  1754.  
  1755.       PROGRESS (1);
  1756.  
  1757.       if (s->s_name[0])
  1758.     {
  1759.       fragS *frag = segment_info[i].frchainP->frch_root;
  1760.       char *buffer;
  1761.  
  1762.       if (s->s_size == 0)
  1763.         s->s_scnptr = 0;
  1764.       else
  1765.         {
  1766.           buffer = xmalloc (s->s_size);
  1767.           s->s_scnptr = *file_cursor;
  1768.         }
  1769.       know (s->s_paddr == paddr);
  1770.  
  1771.       if (strcmp (s->s_name, ".text") == 0)
  1772.         s->s_flags |= STYP_TEXT;
  1773.       else if (strcmp (s->s_name, ".data") == 0)
  1774.         s->s_flags |= STYP_DATA;
  1775.       else if (strcmp (s->s_name, ".bss") == 0)
  1776.         {
  1777.           s->s_scnptr = 0;
  1778.           s->s_flags |= STYP_BSS;
  1779.  
  1780.           /* @@ Should make the i386 and a29k coff targets define
  1781.          COFF_NOLOAD_PROBLEM, and have only one test here.  */
  1782. #ifndef TC_I386
  1783. #ifndef TC_A29K
  1784. #ifndef COFF_NOLOAD_PROBLEM
  1785.           /* Apparently the SVR3 linker (and exec syscall) and UDI
  1786.          mondfe progrem are confused by noload sections.  */
  1787.           s->s_flags |= STYP_NOLOAD;
  1788. #endif
  1789. #endif
  1790. #endif
  1791.         }
  1792.       else if (strcmp (s->s_name, ".lit") == 0)
  1793.         s->s_flags = STYP_LIT | STYP_TEXT;
  1794.       else if (strcmp (s->s_name, ".init") == 0)
  1795.         s->s_flags |= STYP_TEXT;
  1796.       else if (strcmp (s->s_name, ".fini") == 0)
  1797.         s->s_flags |= STYP_TEXT;
  1798.       else if (strncmp (s->s_name, ".comment", 8) == 0)
  1799.         s->s_flags |= STYP_INFO;
  1800.  
  1801.       while (frag)
  1802.         {
  1803.           unsigned int fill_size;
  1804.           switch (frag->fr_type)
  1805.         {
  1806.         case rs_machine_dependent:
  1807.           if (frag->fr_fix)
  1808.             {
  1809.               memcpy (buffer + frag->fr_address,
  1810.                   frag->fr_literal,
  1811.                   (unsigned int) frag->fr_fix);
  1812.               offset += frag->fr_fix;
  1813.             }
  1814.  
  1815.           break;
  1816.         case rs_space:
  1817.           assert (frag->fr_symbol == 0);
  1818.         case rs_fill:
  1819.         case rs_align:
  1820.         case rs_align_code:
  1821.         case rs_org:
  1822.           if (frag->fr_fix)
  1823.             {
  1824.               memcpy (buffer + frag->fr_address,
  1825.                   frag->fr_literal,
  1826.                   (unsigned int) frag->fr_fix);
  1827.               offset += frag->fr_fix;
  1828.             }
  1829.  
  1830.           fill_size = frag->fr_var;
  1831.           if (fill_size && frag->fr_offset > 0)
  1832.             {
  1833.               unsigned int count;
  1834.               unsigned int off = frag->fr_fix;
  1835.               for (count = frag->fr_offset; count; count--)
  1836.             {
  1837.               if (fill_size + frag->fr_address + off <= s->s_size)
  1838.                 {
  1839.                   memcpy (buffer + frag->fr_address + off,
  1840.                       frag->fr_literal + frag->fr_fix,
  1841.                       fill_size);
  1842.                   off += fill_size;
  1843.                   offset += fill_size;
  1844.                 }
  1845.             }
  1846.             }
  1847.           break;
  1848.         case rs_broken_word:
  1849.           break;
  1850.         default:
  1851.           abort ();
  1852.         }
  1853.           frag = frag->fr_next;
  1854.         }
  1855.  
  1856.       if (s->s_size != 0)
  1857.         {
  1858.           if (s->s_scnptr != 0)
  1859.         {
  1860.           bfd_write (buffer, s->s_size, 1, abfd);
  1861.           *file_cursor += s->s_size;
  1862.         }
  1863.           free (buffer);
  1864.         }
  1865.       paddr += s->s_size;
  1866.     }
  1867.     }
  1868. }
  1869.  
  1870. /* Coff file generation & utilities */
  1871.  
  1872. static void
  1873. coff_header_append (abfd, h)
  1874.      bfd * abfd;
  1875.      object_headers * h;
  1876. {
  1877.   unsigned int i;
  1878.   char buffer[1000];
  1879.   char buffero[1000];
  1880.  
  1881.   bfd_seek (abfd, 0, 0);
  1882.  
  1883. #ifndef OBJ_COFF_OMIT_OPTIONAL_HEADER
  1884.   H_SET_MAGIC_NUMBER (h, COFF_MAGIC);
  1885.   H_SET_VERSION_STAMP (h, 0);
  1886.   H_SET_ENTRY_POINT (h, 0);
  1887.   H_SET_TEXT_START (h, segment_info[SEG_E0].frchainP->frch_root->fr_address);
  1888.   H_SET_DATA_START (h, segment_info[SEG_E1].frchainP->frch_root->fr_address);
  1889.   H_SET_SIZEOF_OPTIONAL_HEADER (h, bfd_coff_swap_aouthdr_out(abfd, &h->aouthdr,
  1890.                                  buffero));
  1891. #else /* defined (OBJ_COFF_OMIT_OPTIONAL_HEADER) */
  1892.   H_SET_SIZEOF_OPTIONAL_HEADER (h, 0);
  1893. #endif /* defined (OBJ_COFF_OMIT_OPTIONAL_HEADER) */
  1894.  
  1895.   i = bfd_coff_swap_filehdr_out (abfd, &h->filehdr, buffer);
  1896.  
  1897.   bfd_write (buffer, i, 1, abfd);
  1898.   bfd_write (buffero, H_GET_SIZEOF_OPTIONAL_HEADER (h), 1, abfd);
  1899.  
  1900.   for (i = SEG_E0; i < SEG_LAST; i++)
  1901.     {
  1902. #ifdef COFF_LONG_SECTION_NAMES
  1903.       unsigned long string_size = 4;
  1904. #endif
  1905.  
  1906.       if (segment_info[i].scnhdr.s_name[0])
  1907.     {
  1908.       unsigned int size;
  1909.  
  1910. #ifdef COFF_LONG_SECTION_NAMES
  1911.       /* Support long section names as found in PE.  This code
  1912.              must coordinate with that in write_object_file and
  1913.              w_strings.  */
  1914.       if (strlen (segment_info[i].name) > SCNNMLEN)
  1915.         {
  1916.           memset (segment_info[i].scnhdr.s_name, 0, SCNNMLEN);
  1917.           sprintf (segment_info[i].scnhdr.s_name, "/%lu", string_size);
  1918.           string_size += strlen (segment_info[i].name) + 1;
  1919.         }
  1920. #endif
  1921.  
  1922.       size = bfd_coff_swap_scnhdr_out (abfd,
  1923.                        &(segment_info[i].scnhdr),
  1924.                        buffer);
  1925.       if (size == 0)
  1926.         as_bad ("bfd_coff_swap_scnhdr_out failed");
  1927.       bfd_write (buffer, size, 1, abfd);
  1928.     }
  1929.     }
  1930. }
  1931.  
  1932.  
  1933. char *
  1934. symbol_to_chars (abfd, where, symbolP)
  1935.      bfd * abfd;
  1936.      char *where;
  1937.      symbolS * symbolP;
  1938. {
  1939.   unsigned int numaux = symbolP->sy_symbol.ost_entry.n_numaux;
  1940.   unsigned int i;
  1941.   valueT val;
  1942.  
  1943.   /* Turn any symbols with register attributes into abs symbols */
  1944.   if (S_GET_SEGMENT (symbolP) == reg_section)
  1945.     {
  1946.       S_SET_SEGMENT (symbolP, absolute_section);
  1947.     }
  1948.   /* At the same time, relocate all symbols to their output value */
  1949.  
  1950.   val = (segment_info[S_GET_SEGMENT (symbolP)].scnhdr.s_paddr
  1951.      + S_GET_VALUE (symbolP));
  1952.  
  1953.   S_SET_VALUE (symbolP, val);
  1954.  
  1955.   symbolP->sy_symbol.ost_entry.n_value = val;
  1956.  
  1957.   where += bfd_coff_swap_sym_out (abfd, &symbolP->sy_symbol.ost_entry,
  1958.                   where);
  1959.  
  1960.   for (i = 0; i < numaux; i++)
  1961.     {
  1962.       where += bfd_coff_swap_aux_out (abfd,
  1963.                       &symbolP->sy_symbol.ost_auxent[i],
  1964.                       S_GET_DATA_TYPE (symbolP),
  1965.                       S_GET_STORAGE_CLASS (symbolP),
  1966.                       i, numaux, where);
  1967.     }
  1968.   return where;
  1969.  
  1970. }
  1971.  
  1972. void
  1973. obj_symbol_new_hook (symbolP)
  1974.      symbolS *symbolP;
  1975. {
  1976.   char underscore = 0;        /* Symbol has leading _ */
  1977.  
  1978.   /* Effective symbol */
  1979.   /* Store the pointer in the offset. */
  1980.   S_SET_ZEROES (symbolP, 0L);
  1981.   S_SET_DATA_TYPE (symbolP, T_NULL);
  1982.   S_SET_STORAGE_CLASS (symbolP, 0);
  1983.   S_SET_NUMBER_AUXILIARY (symbolP, 0);
  1984.   /* Additional information */
  1985.   symbolP->sy_symbol.ost_flags = 0;
  1986.   /* Auxiliary entries */
  1987.   memset ((char *) &symbolP->sy_symbol.ost_auxent[0], 0, AUXESZ);
  1988.  
  1989.   if (S_IS_STRING (symbolP))
  1990.     SF_SET_STRING (symbolP);
  1991.   if (!underscore && S_IS_LOCAL (symbolP))
  1992.     SF_SET_LOCAL (symbolP);
  1993. }
  1994.  
  1995. /*
  1996.  * Handle .ln directives.
  1997.  */
  1998.  
  1999. static void
  2000. obj_coff_ln (appline)
  2001.      int appline;
  2002. {
  2003.   int l;
  2004.  
  2005.   if (! appline && def_symbol_in_progress != NULL)
  2006.     {
  2007.       as_warn (".ln pseudo-op inside .def/.endef: ignored.");
  2008.       demand_empty_rest_of_line ();
  2009.       return;
  2010.     }                /* wrong context */
  2011.  
  2012.   l = get_absolute_expression ();
  2013.   c_line_new (0, frag_now_fix (), l, frag_now);
  2014. #ifndef NO_LISTING
  2015.   {
  2016.     extern int listing;
  2017.  
  2018.     if (listing)
  2019.       {
  2020.     if (! appline)
  2021.       l += line_base - 1;
  2022.     listing_source_line ((unsigned int) l);
  2023.       }
  2024.  
  2025.   }
  2026. #endif
  2027.   demand_empty_rest_of_line ();
  2028. }
  2029.  
  2030. /*
  2031.  *            def()
  2032.  *
  2033.  * Handle .def directives.
  2034.  *
  2035.  * One might ask : why can't we symbol_new if the symbol does not
  2036.  * already exist and fill it with debug information.  Because of
  2037.  * the C_EFCN special symbol. It would clobber the value of the
  2038.  * function symbol before we have a chance to notice that it is
  2039.  * a C_EFCN. And a second reason is that the code is more clear this
  2040.  * way. (at least I think it is :-).
  2041.  *
  2042.  */
  2043.  
  2044. #define SKIP_SEMI_COLON()    while (*input_line_pointer++ != ';')
  2045. #define SKIP_WHITESPACES()    while (*input_line_pointer == ' ' || \
  2046.                       *input_line_pointer == '\t') \
  2047.                                          input_line_pointer++;
  2048.  
  2049. static void
  2050. obj_coff_def (what)
  2051.      int what;
  2052. {
  2053.   char name_end;        /* Char after the end of name */
  2054.   char *symbol_name;        /* Name of the debug symbol */
  2055.   char *symbol_name_copy;    /* Temporary copy of the name */
  2056.   unsigned int symbol_name_length;
  2057.  
  2058.   if (def_symbol_in_progress != NULL)
  2059.     {
  2060.       as_warn (".def pseudo-op used inside of .def/.endef: ignored.");
  2061.       demand_empty_rest_of_line ();
  2062.       return;
  2063.     }                /* if not inside .def/.endef */
  2064.  
  2065.   SKIP_WHITESPACES ();
  2066.  
  2067.   def_symbol_in_progress = (symbolS *) obstack_alloc (¬es, sizeof (*def_symbol_in_progress));
  2068.   memset (def_symbol_in_progress, 0, sizeof (*def_symbol_in_progress));
  2069.  
  2070.   symbol_name = input_line_pointer;
  2071.   name_end = get_symbol_end ();
  2072.   symbol_name_length = strlen (symbol_name);
  2073.   symbol_name_copy = xmalloc (symbol_name_length + 1);
  2074.   strcpy (symbol_name_copy, symbol_name);
  2075.  
  2076.   /* Initialize the new symbol */
  2077. #ifdef STRIP_UNDERSCORE
  2078.   S_SET_NAME (def_symbol_in_progress, (*symbol_name_copy == '_'
  2079.                        ? symbol_name_copy + 1
  2080.                        : symbol_name_copy));
  2081. #else /* STRIP_UNDERSCORE */
  2082.   S_SET_NAME (def_symbol_in_progress, symbol_name_copy);
  2083. #endif /* STRIP_UNDERSCORE */
  2084.   /* free(symbol_name_copy); */
  2085.   def_symbol_in_progress->sy_name_offset = (unsigned long) ~0;
  2086.   def_symbol_in_progress->sy_number = ~0;
  2087.   def_symbol_in_progress->sy_frag = &zero_address_frag;
  2088.   S_SET_VALUE (def_symbol_in_progress, 0);
  2089.  
  2090.   if (S_IS_STRING (def_symbol_in_progress))
  2091.     SF_SET_STRING (def_symbol_in_progress);
  2092.  
  2093.   *input_line_pointer = name_end;
  2094.  
  2095.   demand_empty_rest_of_line ();
  2096. }
  2097.  
  2098. unsigned int dim_index;
  2099.  
  2100.  
  2101. static void
  2102. obj_coff_endef (ignore)
  2103.      int ignore;
  2104. {
  2105.   symbolS *symbolP = 0;
  2106.   /* DIM BUG FIX sac@cygnus.com */
  2107.   dim_index = 0;
  2108.   if (def_symbol_in_progress == NULL)
  2109.     {
  2110.       as_warn (".endef pseudo-op used outside of .def/.endef: ignored.");
  2111.       demand_empty_rest_of_line ();
  2112.       return;
  2113.     }                /* if not inside .def/.endef */
  2114.  
  2115.   /* Set the section number according to storage class. */
  2116.   switch (S_GET_STORAGE_CLASS (def_symbol_in_progress))
  2117.     {
  2118.     case C_STRTAG:
  2119.     case C_ENTAG:
  2120.     case C_UNTAG:
  2121.       SF_SET_TAG (def_symbol_in_progress);
  2122.       /* intentional fallthrough */
  2123.     case C_FILE:
  2124.     case C_TPDEF:
  2125.       SF_SET_DEBUG (def_symbol_in_progress);
  2126.       S_SET_SEGMENT (def_symbol_in_progress, SEG_DEBUG);
  2127.       break;
  2128.  
  2129.     case C_EFCN:
  2130.       SF_SET_LOCAL (def_symbol_in_progress);    /* Do not emit this symbol. */
  2131.       /* intentional fallthrough */
  2132.     case C_BLOCK:
  2133.       SF_SET_PROCESS (def_symbol_in_progress);    /* Will need processing before writing */
  2134.       /* intentional fallthrough */
  2135.     case C_FCN:
  2136.       S_SET_SEGMENT (def_symbol_in_progress, SEG_E0);
  2137.  
  2138.       if (strcmp (S_GET_NAME (def_symbol_in_progress), ".bf") == 0)
  2139.     {            /* .bf */
  2140.       if (function_lineoff < 0)
  2141.         {
  2142.           fprintf (stderr, "`.bf' symbol without preceding function\n");
  2143.         }            /* missing function symbol */
  2144.       SA_GET_SYM_LNNOPTR (last_line_symbol) = function_lineoff;
  2145.  
  2146.       SF_SET_PROCESS (last_line_symbol);
  2147.       SF_SET_ADJ_LNNOPTR (last_line_symbol);
  2148.       SF_SET_PROCESS (def_symbol_in_progress);
  2149.       function_lineoff = -1;
  2150.     }
  2151.       /* Value is always set to . */
  2152.       def_symbol_in_progress->sy_frag = frag_now;
  2153.       S_SET_VALUE (def_symbol_in_progress, (valueT) frag_now_fix ());
  2154.       break;
  2155.  
  2156. #ifdef C_AUTOARG
  2157.     case C_AUTOARG:
  2158. #endif /* C_AUTOARG */
  2159.     case C_AUTO:
  2160.     case C_REG:
  2161.     case C_MOS:
  2162.     case C_MOE:
  2163.     case C_MOU:
  2164.     case C_ARG:
  2165.     case C_REGPARM:
  2166.     case C_FIELD:
  2167.     case C_EOS:
  2168.       SF_SET_DEBUG (def_symbol_in_progress);
  2169.       S_SET_SEGMENT (def_symbol_in_progress, absolute_section);
  2170.       break;
  2171.  
  2172.     case C_EXT:
  2173.     case C_STAT:
  2174.     case C_LABEL:
  2175.       /* Valid but set somewhere else (s_comm, s_lcomm, colon) */
  2176.       break;
  2177.  
  2178.     case C_USTATIC:
  2179.     case C_EXTDEF:
  2180.     case C_ULABEL:
  2181.       as_warn ("unexpected storage class %d", S_GET_STORAGE_CLASS (def_symbol_in_progress));
  2182.       break;
  2183.     }                /* switch on storage class */
  2184.  
  2185.   /* Now that we have built a debug symbol, try to find if we should
  2186.      merge with an existing symbol or not.  If a symbol is C_EFCN or
  2187.      absolute_section or untagged SEG_DEBUG it never merges.  We also
  2188.      don't merge labels, which are in a different namespace, nor
  2189.      symbols which have not yet been defined since they are typically
  2190.      unique, nor do we merge tags with non-tags.  */
  2191.  
  2192.   /* Two cases for functions.  Either debug followed by definition or
  2193.      definition followed by debug.  For definition first, we will
  2194.      merge the debug symbol into the definition.  For debug first, the
  2195.      lineno entry MUST point to the definition function or else it
  2196.      will point off into space when crawl_symbols() merges the debug
  2197.      symbol into the real symbol.  Therefor, let's presume the debug
  2198.      symbol is a real function reference. */
  2199.  
  2200.   /* FIXME-SOON If for some reason the definition label/symbol is
  2201.      never seen, this will probably leave an undefined symbol at link
  2202.      time. */
  2203.  
  2204.   if (S_GET_STORAGE_CLASS (def_symbol_in_progress) == C_EFCN
  2205.       || S_GET_STORAGE_CLASS (def_symbol_in_progress) == C_LABEL
  2206.       || (S_GET_SEGMENT (def_symbol_in_progress) == SEG_DEBUG
  2207.       && !SF_GET_TAG (def_symbol_in_progress))
  2208.       || S_GET_SEGMENT (def_symbol_in_progress) == absolute_section
  2209.       || def_symbol_in_progress->sy_value.X_op != O_constant
  2210.       || (symbolP = symbol_find_base (S_GET_NAME (def_symbol_in_progress), DO_NOT_STRIP)) == NULL
  2211.       || (SF_GET_TAG (def_symbol_in_progress) != SF_GET_TAG (symbolP)))
  2212.     {
  2213.       symbol_append (def_symbol_in_progress, symbol_lastP, &symbol_rootP,
  2214.              &symbol_lastP);
  2215.     }
  2216.   else
  2217.     {
  2218.       /* This symbol already exists, merge the newly created symbol
  2219.      into the old one.  This is not mandatory. The linker can
  2220.      handle duplicate symbols correctly. But I guess that it save
  2221.      a *lot* of space if the assembly file defines a lot of
  2222.      symbols. [loic] */
  2223.  
  2224.       /* The debug entry (def_symbol_in_progress) is merged into the
  2225.      previous definition.  */
  2226.  
  2227.       c_symbol_merge (def_symbol_in_progress, symbolP);
  2228.       /* FIXME-SOON Should *def_symbol_in_progress be free'd? xoxorich. */
  2229.       def_symbol_in_progress = symbolP;
  2230.  
  2231.       if (SF_GET_FUNCTION (def_symbol_in_progress)
  2232.       || SF_GET_TAG (def_symbol_in_progress))
  2233.     {
  2234.       /* For functions, and tags, the symbol *must* be where the
  2235.          debug symbol appears.  Move the existing symbol to the
  2236.          current place. */
  2237.       /* If it already is at the end of the symbol list, do nothing */
  2238.       if (def_symbol_in_progress != symbol_lastP)
  2239.         {
  2240.           symbol_remove (def_symbol_in_progress, &symbol_rootP,
  2241.                  &symbol_lastP);
  2242.           symbol_append (def_symbol_in_progress, symbol_lastP,
  2243.                  &symbol_rootP, &symbol_lastP);
  2244.         }            /* if not already in place */
  2245.     }            /* if function */
  2246.     }                /* normal or mergable */
  2247.  
  2248.   if (SF_GET_TAG (def_symbol_in_progress))
  2249.     {
  2250.       symbolS *oldtag;
  2251.  
  2252.       oldtag = symbol_find_base (S_GET_NAME (def_symbol_in_progress),
  2253.                  DO_NOT_STRIP);
  2254.       if (oldtag == NULL || ! SF_GET_TAG (oldtag))
  2255.     tag_insert (S_GET_NAME (def_symbol_in_progress),
  2256.             def_symbol_in_progress);
  2257.     }
  2258.  
  2259.   if (SF_GET_FUNCTION (def_symbol_in_progress))
  2260.     {
  2261.       know (sizeof (def_symbol_in_progress) <= sizeof (long));
  2262.       function_lineoff
  2263.     = c_line_new (def_symbol_in_progress, 0, 0, &zero_address_frag);
  2264.  
  2265.       SF_SET_PROCESS (def_symbol_in_progress);
  2266.  
  2267.       if (symbolP == NULL)
  2268.     {
  2269.       /* That is, if this is the first time we've seen the
  2270.          function... */
  2271.       symbol_table_insert (def_symbol_in_progress);
  2272.     }            /* definition follows debug */
  2273.     }                /* Create the line number entry pointing to the function being defined */
  2274.  
  2275.   def_symbol_in_progress = NULL;
  2276.   demand_empty_rest_of_line ();
  2277. }
  2278.  
  2279. static void
  2280. obj_coff_dim (ignore)
  2281.      int ignore;
  2282. {
  2283.   int dim_index;
  2284.  
  2285.   if (def_symbol_in_progress == NULL)
  2286.     {
  2287.       as_warn (".dim pseudo-op used outside of .def/.endef: ignored.");
  2288.       demand_empty_rest_of_line ();
  2289.       return;
  2290.     }                /* if not inside .def/.endef */
  2291.  
  2292.   S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
  2293.  
  2294.   for (dim_index = 0; dim_index < DIMNUM; dim_index++)
  2295.     {
  2296.       SKIP_WHITESPACES ();
  2297.       SA_SET_SYM_DIMEN (def_symbol_in_progress, dim_index,
  2298.             get_absolute_expression ());
  2299.  
  2300.       switch (*input_line_pointer)
  2301.     {
  2302.     case ',':
  2303.       input_line_pointer++;
  2304.       break;
  2305.  
  2306.     default:
  2307.       as_warn ("badly formed .dim directive ignored");
  2308.       /* intentional fallthrough */
  2309.     case '\n':
  2310.     case ';':
  2311.       dim_index = DIMNUM;
  2312.       break;
  2313.     }
  2314.     }
  2315.  
  2316.   demand_empty_rest_of_line ();
  2317. }
  2318.  
  2319. static void
  2320. obj_coff_line (ignore)
  2321.      int ignore;
  2322. {
  2323.   int this_base;
  2324.   const char *name;
  2325.  
  2326.   if (def_symbol_in_progress == NULL)
  2327.     {
  2328.       obj_coff_ln (0);
  2329.       return;
  2330.     }
  2331.  
  2332.   name = S_GET_NAME (def_symbol_in_progress);
  2333.   this_base = get_absolute_expression ();
  2334.  
  2335.   /* Only .bf symbols indicate the use of a new base line number; the
  2336.      line numbers associated with .ef, .bb, .eb are relative to the
  2337.      start of the containing function.  */
  2338.   if (!strcmp (".bf", name))
  2339.     {
  2340. #if 0 /* XXX Can we ever have line numbers going backwards?  */
  2341.       if (this_base > line_base)
  2342. #endif
  2343.     {
  2344.       line_base = this_base;
  2345.     }
  2346.  
  2347. #ifndef NO_LISTING
  2348.       {
  2349.     extern int listing;
  2350.     if (listing)
  2351.       {
  2352.         listing_source_line ((unsigned int) line_base);
  2353.       }
  2354.       }
  2355. #endif
  2356.     }
  2357.  
  2358.   S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
  2359.   SA_SET_SYM_LNNO (def_symbol_in_progress, this_base);
  2360.  
  2361.   demand_empty_rest_of_line ();
  2362. }
  2363.  
  2364. static void
  2365. obj_coff_size (ignore)
  2366.      int ignore;
  2367. {
  2368.   if (def_symbol_in_progress == NULL)
  2369.     {
  2370.       as_warn (".size pseudo-op used outside of .def/.endef ignored.");
  2371.       demand_empty_rest_of_line ();
  2372.       return;
  2373.     }                /* if not inside .def/.endef */
  2374.  
  2375.   S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
  2376.   SA_SET_SYM_SIZE (def_symbol_in_progress, get_absolute_expression ());
  2377.   demand_empty_rest_of_line ();
  2378. }
  2379.  
  2380. static void
  2381. obj_coff_scl (ignore)
  2382.      int ignore;
  2383. {
  2384.   if (def_symbol_in_progress == NULL)
  2385.     {
  2386.       as_warn (".scl pseudo-op used outside of .def/.endef ignored.");
  2387.       demand_empty_rest_of_line ();
  2388.       return;
  2389.     }                /* if not inside .def/.endef */
  2390.  
  2391.   S_SET_STORAGE_CLASS (def_symbol_in_progress, get_absolute_expression ());
  2392.   demand_empty_rest_of_line ();
  2393. }
  2394.  
  2395. static void
  2396. obj_coff_tag (ignore)
  2397.      int ignore;
  2398. {
  2399.   char *symbol_name;
  2400.   char name_end;
  2401.  
  2402.   if (def_symbol_in_progress == NULL)
  2403.     {
  2404.       as_warn (".tag pseudo-op used outside of .def/.endef ignored.");
  2405.       demand_empty_rest_of_line ();
  2406.       return;
  2407.     }
  2408.  
  2409.   S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
  2410.   symbol_name = input_line_pointer;
  2411.   name_end = get_symbol_end ();
  2412.  
  2413.   /* Assume that the symbol referred to by .tag is always defined.
  2414.      This was a bad assumption.  I've added find_or_make. xoxorich. */
  2415.   SA_SET_SYM_TAGNDX (def_symbol_in_progress,
  2416.              (long) tag_find_or_make (symbol_name));
  2417.   if (SA_GET_SYM_TAGNDX (def_symbol_in_progress) == 0L)
  2418.     {
  2419.       as_warn ("tag not found for .tag %s", symbol_name);
  2420.     }                /* not defined */
  2421.  
  2422.   SF_SET_TAGGED (def_symbol_in_progress);
  2423.   *input_line_pointer = name_end;
  2424.  
  2425.   demand_empty_rest_of_line ();
  2426. }
  2427.  
  2428. static void
  2429. obj_coff_type (ignore)
  2430.      int ignore;
  2431. {
  2432.   if (def_symbol_in_progress == NULL)
  2433.     {
  2434.       as_warn (".type pseudo-op used outside of .def/.endef ignored.");
  2435.       demand_empty_rest_of_line ();
  2436.       return;
  2437.     }                /* if not inside .def/.endef */
  2438.  
  2439.   S_SET_DATA_TYPE (def_symbol_in_progress, get_absolute_expression ());
  2440.  
  2441.   if (ISFCN (S_GET_DATA_TYPE (def_symbol_in_progress)) &&
  2442.       S_GET_STORAGE_CLASS (def_symbol_in_progress) != C_TPDEF)
  2443.     {
  2444.       SF_SET_FUNCTION (def_symbol_in_progress);
  2445.     }                /* is a function */
  2446.  
  2447.   demand_empty_rest_of_line ();
  2448. }
  2449.  
  2450. static void
  2451. obj_coff_val (ignore)
  2452.      int ignore;
  2453. {
  2454.   if (def_symbol_in_progress == NULL)
  2455.     {
  2456.       as_warn (".val pseudo-op used outside of .def/.endef ignored.");
  2457.       demand_empty_rest_of_line ();
  2458.       return;
  2459.     }                /* if not inside .def/.endef */
  2460.  
  2461.   if (is_name_beginner (*input_line_pointer))
  2462.     {
  2463.       char *symbol_name = input_line_pointer;
  2464.       char name_end = get_symbol_end ();
  2465.  
  2466.       if (!strcmp (symbol_name, "."))
  2467.     {
  2468.       def_symbol_in_progress->sy_frag = frag_now;
  2469.       S_SET_VALUE (def_symbol_in_progress, (valueT) frag_now_fix ());
  2470.       /* If the .val is != from the .def (e.g. statics) */
  2471.     }
  2472.       else if (strcmp (S_GET_NAME (def_symbol_in_progress), symbol_name))
  2473.     {
  2474.       def_symbol_in_progress->sy_value.X_op = O_symbol;
  2475.       def_symbol_in_progress->sy_value.X_add_symbol =
  2476.         symbol_find_or_make (symbol_name);
  2477.       def_symbol_in_progress->sy_value.X_op_symbol = NULL;
  2478.       def_symbol_in_progress->sy_value.X_add_number = 0;
  2479.  
  2480.       /* If the segment is undefined when the forward reference is
  2481.          resolved, then copy the segment id from the forward
  2482.          symbol.  */
  2483.       SF_SET_GET_SEGMENT (def_symbol_in_progress);
  2484.  
  2485.       /* FIXME: gcc can generate address expressions
  2486.          here in unusual cases (search for "obscure"
  2487.          in sdbout.c).  We just ignore the offset
  2488.          here, thus generating incorrect debugging
  2489.          information.  We ignore the rest of the
  2490.          line just below.  */
  2491.     }
  2492.       /* Otherwise, it is the name of a non debug symbol and
  2493.      its value will be calculated later. */
  2494.       *input_line_pointer = name_end;
  2495.  
  2496.       /* FIXME: this is to avoid an error message in the
  2497.      FIXME case mentioned just above.  */
  2498.       while (! is_end_of_line[(unsigned char) *input_line_pointer])
  2499.     ++input_line_pointer;
  2500.     }
  2501.   else
  2502.     {
  2503.       S_SET_VALUE (def_symbol_in_progress,
  2504.            (valueT) get_absolute_expression ());
  2505.     }                /* if symbol based */
  2506.  
  2507.   demand_empty_rest_of_line ();
  2508. }
  2509.  
  2510. #ifdef TE_PE
  2511.  
  2512. /* Handle the .linkonce pseudo-op.  This is parsed by s_linkonce in
  2513.    read.c, which then calls this object file format specific routine.  */
  2514.  
  2515. void
  2516. obj_coff_pe_handle_link_once (type)
  2517.      enum linkonce_type type;
  2518. {
  2519.   seg_info (now_seg)->scnhdr.s_flags |= IMAGE_SCN_LNK_COMDAT;
  2520.  
  2521.   /* We store the type in the seg_info structure, and use it to set up
  2522.      the auxiliary entry for the section symbol in c_section_symbol.  */
  2523.   seg_info (now_seg)->linkonce = type;
  2524. }
  2525.  
  2526. #endif /* TE_PE */
  2527.  
  2528. void
  2529. obj_read_begin_hook ()
  2530. {
  2531.   /* These had better be the same.  Usually 18 bytes. */
  2532. #ifndef BFD_HEADERS
  2533.   know (sizeof (SYMENT) == sizeof (AUXENT));
  2534.   know (SYMESZ == AUXESZ);
  2535. #endif
  2536.   tag_init ();
  2537. }
  2538.  
  2539. /* This function runs through the symbol table and puts all the
  2540.    externals onto another chain */
  2541.  
  2542. /* The chain of globals.  */
  2543. symbolS *symbol_globalP;
  2544. symbolS *symbol_global_lastP;
  2545.  
  2546. /* The chain of externals */
  2547. symbolS *symbol_externP;
  2548. symbolS *symbol_extern_lastP;
  2549.  
  2550. stack *block_stack;
  2551. symbolS *last_functionP;
  2552. static symbolS *last_bfP;
  2553. symbolS *last_tagP;
  2554.  
  2555. static unsigned int
  2556. yank_symbols ()
  2557. {
  2558.   symbolS *symbolP;
  2559.   unsigned int symbol_number = 0;
  2560.   unsigned int last_file_symno = 0;
  2561.  
  2562.   struct filename_list *filename_list_scan = filename_list_head;
  2563.  
  2564.   for (symbolP = symbol_rootP;
  2565.        symbolP;
  2566.        symbolP = symbolP ? symbol_next (symbolP) : symbol_rootP)
  2567.     {
  2568.       if (symbolP->sy_mri_common)
  2569.     {
  2570.       if (S_GET_STORAGE_CLASS (symbolP) == C_EXT)
  2571.         as_bad ("%s: global symbols not supported in common sections",
  2572.             S_GET_NAME (symbolP));
  2573.       symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
  2574.       continue;
  2575.     }
  2576.  
  2577.       if (!SF_GET_DEBUG (symbolP))
  2578.     {
  2579.       /* Debug symbols do not need all this rubbish */
  2580.       symbolS *real_symbolP;
  2581.  
  2582.       /* L* and C_EFCN symbols never merge. */
  2583.       if (!SF_GET_LOCAL (symbolP)
  2584.           && !SF_GET_STATICS (symbolP)
  2585.           && S_GET_STORAGE_CLASS (symbolP) != C_LABEL
  2586.           && symbolP->sy_value.X_op == O_constant
  2587.           && (real_symbolP = symbol_find_base (S_GET_NAME (symbolP), DO_NOT_STRIP))
  2588.           && real_symbolP != symbolP)
  2589.         {
  2590.           /* FIXME-SOON: where do dups come from?
  2591.          Maybe tag references before definitions? xoxorich. */
  2592.           /* Move the debug data from the debug symbol to the
  2593.          real symbol. Do NOT do the oposite (i.e. move from
  2594.          real symbol to debug symbol and remove real symbol from the
  2595.          list.) Because some pointers refer to the real symbol
  2596.          whereas no pointers refer to the debug symbol. */
  2597.           c_symbol_merge (symbolP, real_symbolP);
  2598.           /* Replace the current symbol by the real one */
  2599.           /* The symbols will never be the last or the first
  2600.          because : 1st symbol is .file and 3 last symbols are
  2601.          .text, .data, .bss */
  2602.           symbol_remove (real_symbolP, &symbol_rootP, &symbol_lastP);
  2603.           symbol_insert (real_symbolP, symbolP, &symbol_rootP, &symbol_lastP);
  2604.           symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
  2605.           symbolP = real_symbolP;
  2606.         }            /* if not local but dup'd */
  2607.  
  2608.       if (flag_readonly_data_in_text && (S_GET_SEGMENT (symbolP) == SEG_E1))
  2609.         {
  2610.           S_SET_SEGMENT (symbolP, SEG_E0);
  2611.         }            /* push data into text */
  2612.  
  2613.       resolve_symbol_value (symbolP);
  2614.  
  2615.       if (S_GET_STORAGE_CLASS (symbolP) == C_NULL)
  2616.         {
  2617.           if (!S_IS_DEFINED (symbolP) && !SF_GET_LOCAL (symbolP))
  2618.         {
  2619.           S_SET_EXTERNAL (symbolP);
  2620.         }
  2621.           else if (S_GET_SEGMENT (symbolP) == SEG_E0)
  2622.         {
  2623.           S_SET_STORAGE_CLASS (symbolP, C_LABEL);
  2624.         }
  2625.           else
  2626.         {
  2627.           S_SET_STORAGE_CLASS (symbolP, C_STAT);
  2628.         }
  2629.         }
  2630.  
  2631.       /* Mainly to speed up if not -g */
  2632.       if (SF_GET_PROCESS (symbolP))
  2633.         {
  2634.           /* Handle the nested blocks auxiliary info. */
  2635.           if (S_GET_STORAGE_CLASS (symbolP) == C_BLOCK)
  2636.         {
  2637.           if (!strcmp (S_GET_NAME (symbolP), ".bb"))
  2638.             stack_push (block_stack, (char *) &symbolP);
  2639.           else
  2640.             {        /* .eb */
  2641.               register symbolS *begin_symbolP;
  2642.               begin_symbolP = *(symbolS **) stack_pop (block_stack);
  2643.               if (begin_symbolP == (symbolS *) 0)
  2644.             as_warn ("mismatched .eb");
  2645.               else
  2646.             SA_SET_SYM_ENDNDX (begin_symbolP, symbol_number + 2);
  2647.             }
  2648.         }
  2649.           /* If we are able to identify the type of a function, and we
  2650.            are out of a function (last_functionP == 0) then, the
  2651.            function symbol will be associated with an auxiliary
  2652.            entry. */
  2653.           if (last_functionP == (symbolS *) 0 &&
  2654.           SF_GET_FUNCTION (symbolP))
  2655.         {
  2656.           last_functionP = symbolP;
  2657.  
  2658.           if (S_GET_NUMBER_AUXILIARY (symbolP) < 1)
  2659.             {
  2660.               S_SET_NUMBER_AUXILIARY (symbolP, 1);
  2661.             }        /* make it at least 1 */
  2662.  
  2663.           /* Clobber possible stale .dim information. */
  2664. #if 0
  2665.           /* Iffed out by steve - this fries the lnnoptr info too */
  2666.           bzero (symbolP->sy_symbol.ost_auxent[0].x_sym.x_fcnary.x_ary.x_dimen,
  2667.              sizeof (symbolP->sy_symbol.ost_auxent[0].x_sym.x_fcnary.x_ary.x_dimen));
  2668. #endif
  2669.         }
  2670.           if (S_GET_STORAGE_CLASS (symbolP) == C_FCN)
  2671.         {
  2672.           if (strcmp (S_GET_NAME (symbolP), ".bf") == 0)
  2673.             {
  2674.               if (last_bfP != NULL)
  2675.             SA_SET_SYM_ENDNDX (last_bfP, symbol_number);
  2676.               last_bfP = symbolP;
  2677.             }
  2678.         }
  2679.           else if (S_GET_STORAGE_CLASS (symbolP) == C_EFCN)
  2680.         {
  2681.           /* I don't even know if this is needed for sdb. But
  2682.              the standard assembler generates it, so...  */
  2683.           if (last_functionP == (symbolS *) 0)
  2684.             as_fatal ("C_EFCN symbol out of scope");
  2685.           SA_SET_SYM_FSIZE (last_functionP,
  2686.                     (long) (S_GET_VALUE (symbolP) -
  2687.                         S_GET_VALUE (last_functionP)));
  2688.           SA_SET_SYM_ENDNDX (last_functionP, symbol_number);
  2689.          last_functionP = (symbolS *) 0;
  2690.         }
  2691.         }
  2692.     }
  2693.       else if (SF_GET_TAG (symbolP))
  2694.     {
  2695.       /* First descriptor of a structure must point to
  2696.            the first slot after the structure description. */
  2697.       last_tagP = symbolP;
  2698.  
  2699.     }
  2700.       else if (S_GET_STORAGE_CLASS (symbolP) == C_EOS)
  2701.     {
  2702.       /* +2 take in account the current symbol */
  2703.       SA_SET_SYM_ENDNDX (last_tagP, symbol_number + 2);
  2704.     }
  2705.       else if (S_GET_STORAGE_CLASS (symbolP) == C_FILE)
  2706.     {
  2707.       /* If the filename was too long to fit in the
  2708.          auxent, put it in the string table */
  2709.       if (SA_GET_FILE_FNAME_ZEROS (symbolP) == 0
  2710.           && SA_GET_FILE_FNAME_OFFSET (symbolP) != 0)
  2711.         {
  2712.           SA_SET_FILE_FNAME_OFFSET (symbolP, string_byte_count);
  2713.           string_byte_count += strlen (filename_list_scan->filename) + 1;
  2714.           filename_list_scan = filename_list_scan->next;
  2715.         }
  2716.       if (S_GET_VALUE (symbolP))
  2717.         {
  2718.           S_SET_VALUE (symbolP, last_file_symno);
  2719.           last_file_symno = symbol_number;
  2720.         }            /* no one points at the first .file symbol */
  2721.     }            /* if debug or tag or eos or file */
  2722.  
  2723.       /* We must put the external symbols apart. The loader
  2724.      does not bomb if we do not. But the references in
  2725.      the endndx field for a .bb symbol are not corrected
  2726.      if an external symbol is removed between .bb and .be.
  2727.      I.e in the following case :
  2728.      [20] .bb endndx = 22
  2729.      [21] foo external
  2730.      [22] .be
  2731.      ld will move the symbol 21 to the end of the list but
  2732.      endndx will still be 22 instead of 21. */
  2733.  
  2734.  
  2735.       if (SF_GET_LOCAL (symbolP))
  2736.     {
  2737.       /* remove C_EFCN and LOCAL (L...) symbols */
  2738.       /* next pointer remains valid */
  2739.       symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
  2740.  
  2741.     }
  2742.       else if (symbolP->sy_value.X_op == O_symbol
  2743.            && (! S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP)))
  2744.     {
  2745.       /* Skip symbols which were equated to undefined or common
  2746.          symbols.  */
  2747.       symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
  2748.     }
  2749.       else if (!S_IS_DEFINED (symbolP)
  2750.            && !S_IS_DEBUG (symbolP)
  2751.            && !SF_GET_STATICS (symbolP) &&
  2752.            S_GET_STORAGE_CLASS (symbolP) == C_EXT)
  2753.     {            /* C_EXT && !SF_GET_FUNCTION(symbolP))  */
  2754.       /* if external, Remove from the list */
  2755.       symbolS *hold = symbol_previous (symbolP);
  2756.  
  2757.       symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
  2758.       symbol_clear_list_pointers (symbolP);
  2759.       symbol_append (symbolP, symbol_extern_lastP, &symbol_externP, &symbol_extern_lastP);
  2760.       symbolP = hold;
  2761.     }
  2762.       else if (! S_IS_DEBUG (symbolP)
  2763.            && ! SF_GET_STATICS (symbolP)
  2764.            && ! SF_GET_FUNCTION (symbolP)
  2765.            && S_GET_STORAGE_CLASS (symbolP) == C_EXT)
  2766.     {
  2767.       symbolS *hold = symbol_previous (symbolP);
  2768.  
  2769.       /* The O'Reilly COFF book says that defined global symbols
  2770.              come at the end of the symbol table, just before
  2771.              undefined global symbols.  */
  2772.  
  2773.       symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
  2774.       symbol_clear_list_pointers (symbolP);
  2775.       symbol_append (symbolP, symbol_global_lastP, &symbol_globalP,
  2776.              &symbol_global_lastP);
  2777.       symbolP = hold;
  2778.     }
  2779.       else
  2780.     {
  2781.       if (SF_GET_STRING (symbolP))
  2782.         {
  2783.           symbolP->sy_name_offset = string_byte_count;
  2784.           string_byte_count += strlen (S_GET_NAME (symbolP)) + 1;
  2785.         }
  2786.       else
  2787.         {
  2788.           symbolP->sy_name_offset = 0;
  2789.         }            /* fix "long" names */
  2790.  
  2791.       symbolP->sy_number = symbol_number;
  2792.       symbol_number += 1 + S_GET_NUMBER_AUXILIARY (symbolP);
  2793.     }            /* if local symbol */
  2794.     }                /* traverse the symbol list */
  2795.   return symbol_number;
  2796.  
  2797. }
  2798.  
  2799.  
  2800. static unsigned int
  2801. glue_symbols (head, tail)
  2802.      symbolS **head;
  2803.      symbolS **tail;
  2804. {
  2805.   unsigned int symbol_number = 0;
  2806.   symbolS *symbolP;
  2807.  
  2808.   for (symbolP = *head; *head != NULL;)
  2809.     {
  2810.       symbolS *tmp = *head;
  2811.  
  2812.       /* append */
  2813.       symbol_remove (tmp, head, tail);
  2814.       symbol_append (tmp, symbol_lastP, &symbol_rootP, &symbol_lastP);
  2815.  
  2816.       /* and process */
  2817.       if (SF_GET_STRING (tmp))
  2818.     {
  2819.       tmp->sy_name_offset = string_byte_count;
  2820.       string_byte_count += strlen (S_GET_NAME (tmp)) + 1;
  2821.     }
  2822.       else
  2823.     {
  2824.       tmp->sy_name_offset = 0;
  2825.     }            /* fix "long" names */
  2826.  
  2827.       tmp->sy_number = symbol_number;
  2828.       symbol_number += 1 + S_GET_NUMBER_AUXILIARY (tmp);
  2829.     }                /* append the entire extern chain */
  2830.  
  2831.   return symbol_number;
  2832. }
  2833.  
  2834. static unsigned int
  2835. tie_tags ()
  2836. {
  2837.   unsigned int symbol_number = 0;
  2838.  
  2839.   symbolS *symbolP;
  2840.   for (symbolP = symbol_rootP; symbolP; symbolP =
  2841.        symbol_next (symbolP))
  2842.     {
  2843.       symbolP->sy_number = symbol_number;
  2844.  
  2845.  
  2846.  
  2847.       if (SF_GET_TAGGED (symbolP))
  2848.     {
  2849.       SA_SET_SYM_TAGNDX
  2850.         (symbolP,
  2851.          ((symbolS *) SA_GET_SYM_TAGNDX (symbolP))->sy_number);
  2852.     }
  2853.  
  2854.       symbol_number += 1 + S_GET_NUMBER_AUXILIARY (symbolP);
  2855.     }
  2856.   return symbol_number;
  2857.  
  2858. }
  2859.  
  2860. static void
  2861. crawl_symbols (h, abfd)
  2862.      object_headers *h;
  2863.      bfd * abfd;
  2864. {
  2865.   unsigned int i;
  2866.  
  2867.   /* Initialize the stack used to keep track of the matching .bb .be */
  2868.  
  2869.   block_stack = stack_init (512, sizeof (symbolS *));
  2870.  
  2871.   /* The symbol list should be ordered according to the following sequence
  2872.    * order :
  2873.    * . .file symbol
  2874.    * . debug entries for functions
  2875.    * . fake symbols for the sections, including.text .data and .bss
  2876.    * . defined symbols
  2877.    * . undefined symbols
  2878.    * But this is not mandatory. The only important point is to put the
  2879.    * undefined symbols at the end of the list.
  2880.    */
  2881.  
  2882.   if (symbol_rootP == NULL
  2883.       || S_GET_STORAGE_CLASS (symbol_rootP) != C_FILE)
  2884.     {
  2885.       c_dot_file_symbol ("fake");
  2886.     }
  2887.   /* Is there a .file symbol ? If not insert one at the beginning. */
  2888.  
  2889.   /*
  2890.    * Build up static symbols for the sections, they are filled in later
  2891.    */
  2892.  
  2893.  
  2894.   for (i = SEG_E0; i < SEG_LAST; i++)
  2895.     if (segment_info[i].scnhdr.s_name[0])
  2896.       segment_info[i].dot = c_section_symbol (segment_info[i].name,
  2897.                           i - SEG_E0 + 1);
  2898.  
  2899.   /* Take all the externals out and put them into another chain */
  2900.   H_SET_SYMBOL_TABLE_SIZE (h, yank_symbols ());
  2901.   /* Take the externals and glue them onto the end.*/
  2902.   H_SET_SYMBOL_TABLE_SIZE (h,
  2903.                (H_GET_SYMBOL_COUNT (h)
  2904.                 + glue_symbols (&symbol_globalP,
  2905.                         &symbol_global_lastP)
  2906.                 + glue_symbols (&symbol_externP,
  2907.                         &symbol_extern_lastP)));
  2908.  
  2909.   H_SET_SYMBOL_TABLE_SIZE (h, tie_tags ());
  2910.   know (symbol_globalP == NULL);
  2911.   know (symbol_global_lastP == NULL);
  2912.   know (symbol_externP == NULL);
  2913.   know (symbol_extern_lastP == NULL);
  2914. }
  2915.  
  2916. /*
  2917.  * Find strings by crawling along symbol table chain.
  2918.  */
  2919.  
  2920. void
  2921. w_strings (where)
  2922.      char *where;
  2923. {
  2924.   symbolS *symbolP;
  2925.   struct filename_list *filename_list_scan = filename_list_head;
  2926.  
  2927.   /* Gotta do md_ byte-ordering stuff for string_byte_count first - KWK */
  2928.   md_number_to_chars (where, (valueT) string_byte_count, 4);
  2929.   where += 4;
  2930.  
  2931. #ifdef COFF_LONG_SECTION_NAMES
  2932.   /* Support long section names as found in PE.  This code must
  2933.      coordinate with that in coff_header_append and write_object_file.  */
  2934.   {
  2935.     unsigned int i;
  2936.  
  2937.     for (i = SEG_E0; i < SEG_LAST; i++)
  2938.       {
  2939.     if (segment_info[i].scnhdr.s_name[0]
  2940.         && strlen (segment_info[i].name) > SCNNMLEN)
  2941.       {
  2942.         unsigned int size;
  2943.  
  2944.         size = strlen (segment_info[i].name) + 1;
  2945.         memcpy (where, segment_info[i].name, size);
  2946.         where += size;
  2947.       }
  2948.       }
  2949.   }
  2950. #endif /* COFF_LONG_SECTION_NAMES */
  2951.  
  2952.   for (symbolP = symbol_rootP;
  2953.        symbolP;
  2954.        symbolP = symbol_next (symbolP))
  2955.     {
  2956.       unsigned int size;
  2957.  
  2958.       if (SF_GET_STRING (symbolP))
  2959.     {
  2960.       size = strlen (S_GET_NAME (symbolP)) + 1;
  2961.       memcpy (where, S_GET_NAME (symbolP), size);
  2962.       where += size;
  2963.     }
  2964.       if (S_GET_STORAGE_CLASS (symbolP) == C_FILE
  2965.       && SA_GET_FILE_FNAME_ZEROS (symbolP) == 0
  2966.       && SA_GET_FILE_FNAME_OFFSET (symbolP) != 0)
  2967.     {
  2968.       size = strlen (filename_list_scan->filename) + 1;
  2969.       memcpy (where, filename_list_scan->filename, size);
  2970.       filename_list_scan = filename_list_scan ->next;
  2971.       where += size;
  2972.     }
  2973.     }
  2974. }
  2975.  
  2976. static void
  2977. do_linenos_for (abfd, h, file_cursor)
  2978.      bfd * abfd;
  2979.      object_headers * h;
  2980.      unsigned long *file_cursor;
  2981. {
  2982.   unsigned int idx;
  2983.   unsigned long start = *file_cursor;
  2984.  
  2985.   for (idx = SEG_E0; idx < SEG_LAST; idx++)
  2986.     {
  2987.       segment_info_type *s = segment_info + idx;
  2988.  
  2989.  
  2990.       if (s->scnhdr.s_nlnno != 0)
  2991.     {
  2992.       struct lineno_list *line_ptr;
  2993.  
  2994.       struct external_lineno *buffer =
  2995.       (struct external_lineno *) xmalloc (s->scnhdr.s_nlnno * LINESZ);
  2996.  
  2997.       struct external_lineno *dst = buffer;
  2998.  
  2999.       /* Run through the table we've built and turn it into its external
  3000.      form, take this chance to remove duplicates */
  3001.  
  3002.       for (line_ptr = s->lineno_list_head;
  3003.            line_ptr != (struct lineno_list *) NULL;
  3004.            line_ptr = line_ptr->next)
  3005.         {
  3006.  
  3007.           if (line_ptr->line.l_lnno == 0)
  3008.         {
  3009.           /* Turn a pointer to a symbol into the symbols' index */
  3010.           line_ptr->line.l_addr.l_symndx =
  3011.             ((symbolS *) line_ptr->line.l_addr.l_symndx)->sy_number;
  3012.         }
  3013.           else
  3014.         {
  3015.           line_ptr->line.l_addr.l_paddr += ((struct frag *) (line_ptr->frag))->fr_address;
  3016.         }
  3017.  
  3018.  
  3019.           (void) bfd_coff_swap_lineno_out (abfd, &(line_ptr->line), dst);
  3020.           dst++;
  3021.  
  3022.         }
  3023.  
  3024.       s->scnhdr.s_lnnoptr = *file_cursor;
  3025.  
  3026.       bfd_write (buffer, 1, s->scnhdr.s_nlnno * LINESZ, abfd);
  3027.       free (buffer);
  3028.  
  3029.       *file_cursor += s->scnhdr.s_nlnno * LINESZ;
  3030.     }
  3031.     }
  3032.   H_SET_LINENO_SIZE (h, *file_cursor - start);
  3033. }
  3034.  
  3035.  
  3036. /* Now we run through the list of frag chains in a segment and
  3037.    make all the subsegment frags appear at the end of the
  3038.    list, as if the seg 0 was extra long */
  3039.  
  3040. static void
  3041. remove_subsegs ()
  3042. {
  3043.   unsigned int i;
  3044.  
  3045.   for (i = SEG_E0; i < SEG_UNKNOWN; i++)
  3046.     {
  3047.       frchainS *head = segment_info[i].frchainP;
  3048.       fragS dummy;
  3049.       fragS *prev_frag = &dummy;
  3050.  
  3051.       while (head && head->frch_seg == i)
  3052.     {
  3053.       prev_frag->fr_next = head->frch_root;
  3054.       prev_frag = head->frch_last;
  3055.       head = head->frch_next;
  3056.     }
  3057.       prev_frag->fr_next = 0;
  3058.     }
  3059. }
  3060.  
  3061. unsigned long machine;
  3062. int coff_flags;
  3063. extern void
  3064. write_object_file ()
  3065. {
  3066.   int i;
  3067.   const char *name;
  3068.   struct frchain *frchain_ptr;
  3069.  
  3070.   object_headers headers;
  3071.   unsigned long file_cursor;
  3072.   bfd *abfd;
  3073.   unsigned int addr;
  3074.   abfd = bfd_openw (out_file_name, TARGET_FORMAT);
  3075.  
  3076.  
  3077.   if (abfd == 0)
  3078.     {
  3079.       as_perror ("FATAL: Can't create %s", out_file_name);
  3080.       exit (EXIT_FAILURE);
  3081.     }
  3082.   bfd_set_format (abfd, bfd_object);
  3083.   bfd_set_arch_mach (abfd, BFD_ARCH, machine);
  3084.  
  3085.   string_byte_count = 4;
  3086.  
  3087.   for (frchain_ptr = frchain_root;
  3088.        frchain_ptr != (struct frchain *) NULL;
  3089.        frchain_ptr = frchain_ptr->frch_next)
  3090.     {
  3091.       /* Run through all the sub-segments and align them up.  Also
  3092.      close any open frags.  We tack a .fill onto the end of the
  3093.      frag chain so that any .align's size can be worked by looking
  3094.      at the next frag.  */
  3095.  
  3096.       subseg_set (frchain_ptr->frch_seg, frchain_ptr->frch_subseg);
  3097. #ifndef SUB_SEGMENT_ALIGN
  3098. #define SUB_SEGMENT_ALIGN(SEG) 1
  3099. #endif
  3100. #ifdef md_do_align
  3101.       {
  3102.     static char nop = NOP_OPCODE;
  3103.     md_do_align (SUB_SEGMENT_ALIGN (now_seg), &nop, 1, alignment_done);
  3104.       }
  3105. #endif
  3106.       frag_align (SUB_SEGMENT_ALIGN (now_seg), NOP_OPCODE);
  3107. #ifdef md_do_align
  3108.     alignment_done:
  3109. #endif
  3110.       frag_wane (frag_now);
  3111.       frag_now->fr_fix = 0;
  3112.       know (frag_now->fr_next == NULL);
  3113.     }
  3114.  
  3115.  
  3116.   remove_subsegs ();
  3117.  
  3118.  
  3119.   for (i = SEG_E0; i < SEG_UNKNOWN; i++)
  3120.     {
  3121.       relax_segment (segment_info[i].frchainP->frch_root, i);
  3122.     }
  3123.  
  3124.   H_SET_NUMBER_OF_SECTIONS (&headers, 0);
  3125.  
  3126.   /* Find out how big the sections are, and set the addresses.  */
  3127.   addr = 0;
  3128.   for (i = SEG_E0; i < SEG_UNKNOWN; i++)
  3129.     {
  3130.       long size;
  3131.  
  3132.       segment_info[i].scnhdr.s_paddr = addr;
  3133.       segment_info[i].scnhdr.s_vaddr = addr;
  3134.  
  3135.       if (segment_info[i].scnhdr.s_name[0])
  3136.     {
  3137.       H_SET_NUMBER_OF_SECTIONS (&headers,
  3138.                     H_GET_NUMBER_OF_SECTIONS (&headers) + 1);
  3139.  
  3140. #ifdef COFF_LONG_SECTION_NAMES
  3141.       /* Support long section names as found in PE.  This code
  3142.          must coordinate with that in coff_header_append and
  3143.          w_strings.  */
  3144.       {
  3145.         unsigned int len;
  3146.  
  3147.         len = strlen (segment_info[i].name);
  3148.         if (len > SCNNMLEN)
  3149.           string_byte_count += len + 1;
  3150.       }
  3151. #endif /* COFF_LONG_SECTION_NAMES */
  3152.     }
  3153.  
  3154.       size = size_section (abfd, (unsigned int) i);
  3155.       addr += size;
  3156.  
  3157.       /* I think the section alignment is only used on the i960; the
  3158.      i960 needs it, and it should do no harm on other targets.  */
  3159.       segment_info[i].scnhdr.s_align = 1 << section_alignment[i];
  3160.  
  3161.       if (i == SEG_E0)
  3162.     H_SET_TEXT_SIZE (&headers, size);
  3163.       else if (i == SEG_E1)
  3164.     H_SET_DATA_SIZE (&headers, size);
  3165.       else if (i == SEG_E2)
  3166.     H_SET_BSS_SIZE (&headers, size);
  3167.     }
  3168.  
  3169.   /* Turn the gas native symbol table shape into a coff symbol table */
  3170.   crawl_symbols (&headers, abfd);
  3171.  
  3172.   if (string_byte_count == 4)
  3173.     string_byte_count = 0;
  3174.  
  3175.   H_SET_STRING_SIZE (&headers, string_byte_count);
  3176.  
  3177. #ifdef tc_frob_file
  3178.   tc_frob_file ();
  3179. #endif
  3180.  
  3181.   for (i = SEG_E0; i < SEG_UNKNOWN; i++)
  3182.     {
  3183.       fixup_mdeps (segment_info[i].frchainP->frch_root, &headers, i);
  3184.       fixup_segment (&segment_info[i], i);
  3185.     }
  3186.  
  3187.   /* Look for ".stab" segments and fill in their initial symbols
  3188.      correctly. */
  3189.   for (i = SEG_E0; i < SEG_UNKNOWN; i++)
  3190.     {
  3191.       name = segment_info[i].name;
  3192.  
  3193.       if (name != NULL
  3194.       && strncmp (".stab", name, 5) == 0
  3195.       && strncmp (".stabstr", name, 8) != 0)
  3196.     adjust_stab_section (abfd, i);
  3197.     }
  3198.  
  3199.   file_cursor = H_GET_TEXT_FILE_OFFSET (&headers);
  3200.  
  3201.   bfd_seek (abfd, (file_ptr) file_cursor, 0);
  3202.  
  3203.   /* Plant the data */
  3204.  
  3205.   fill_section (abfd, &headers, &file_cursor);
  3206.  
  3207.   do_relocs_for (abfd, &headers, &file_cursor);
  3208.  
  3209.   do_linenos_for (abfd, &headers, &file_cursor);
  3210.  
  3211.   H_SET_FILE_MAGIC_NUMBER (&headers, COFF_MAGIC);
  3212. #ifndef OBJ_COFF_OMIT_TIMESTAMP
  3213.   H_SET_TIME_STAMP (&headers, (long)time((time_t *)0));
  3214. #else
  3215.   H_SET_TIME_STAMP (&headers, 0);
  3216. #endif
  3217. #ifdef TC_COFF_SET_MACHINE
  3218.   TC_COFF_SET_MACHINE (&headers);
  3219. #endif
  3220.  
  3221. #ifndef COFF_FLAGS
  3222. #define COFF_FLAGS 0
  3223. #endif
  3224.  
  3225. #ifdef KEEP_RELOC_INFO
  3226.   H_SET_FLAGS (&headers, ((H_GET_LINENO_SIZE(&headers) ? 0 : F_LNNO) |
  3227.               COFF_FLAGS | coff_flags));
  3228. #else
  3229.   H_SET_FLAGS (&headers, ((H_GET_LINENO_SIZE(&headers)     ? 0 : F_LNNO)   |
  3230.               (H_GET_RELOCATION_SIZE(&headers) ? 0 : F_RELFLG) |
  3231.               COFF_FLAGS | coff_flags));
  3232. #endif
  3233.  
  3234.   {
  3235.     unsigned int symtable_size = H_GET_SYMBOL_TABLE_SIZE (&headers);
  3236.     char *buffer1 = xmalloc (symtable_size + string_byte_count + 1);
  3237.  
  3238.     H_SET_SYMBOL_TABLE_POINTER (&headers, bfd_tell (abfd));
  3239.     w_symbols (abfd, buffer1, symbol_rootP);
  3240.     if (string_byte_count > 0)
  3241.       w_strings (buffer1 + symtable_size);
  3242.     bfd_write (buffer1, 1, symtable_size + string_byte_count, abfd);
  3243.     free (buffer1);
  3244.   }
  3245.  
  3246.   coff_header_append (abfd, &headers);
  3247. #if 0
  3248.   /* Recent changes to write need this, but where it should
  3249.      go is up to Ken.. */
  3250.   if (bfd_close_all_done (abfd) == false)
  3251.     as_fatal ("Can't close %s: %s", out_file_name,
  3252.           bfd_errmsg (bfd_get_error ()));
  3253. #else
  3254.   {
  3255.     extern bfd *stdoutput;
  3256.     stdoutput = abfd;
  3257.   }
  3258. #endif
  3259.  
  3260. }
  3261.  
  3262. /* Add a new segment.  This is called from subseg_new via the
  3263.    obj_new_segment macro.  */
  3264.  
  3265. segT
  3266. obj_coff_add_segment (name)
  3267.      const char *name;
  3268. {
  3269.   unsigned int i;
  3270.  
  3271. #ifndef COFF_LONG_SECTION_NAMES
  3272.   char buf[SCNNMLEN + 1];
  3273.  
  3274.   strncpy (buf, name, SCNNMLEN);
  3275.   buf[SCNNMLEN] = '\0';
  3276.   name = buf;
  3277. #endif
  3278.  
  3279.   for (i = SEG_E0; i < SEG_LAST && segment_info[i].scnhdr.s_name[0]; i++)
  3280.     if (strcmp (name, segment_info[i].name) == 0)
  3281.       return (segT) i;
  3282.  
  3283.   if (i == SEG_LAST)
  3284.     {
  3285.       as_bad ("Too many new sections; can't add \"%s\"", name);
  3286.       return now_seg;
  3287.     }
  3288.  
  3289.   /* Add a new section.  */
  3290.   strncpy (segment_info[i].scnhdr.s_name, name,
  3291.        sizeof (segment_info[i].scnhdr.s_name));
  3292.   segment_info[i].scnhdr.s_flags = STYP_REG;
  3293.   segment_info[i].name = xstrdup (name);
  3294.  
  3295.   return (segT) i;
  3296. }
  3297.  
  3298. /*
  3299.  * implement the .section pseudo op:
  3300.  *    .section name {, "flags"}
  3301.  *                ^         ^
  3302.  *                |         +--- optional flags: 'b' for bss
  3303.  *                |                              'i' for info
  3304.  *                +-- section name               'l' for lib
  3305.  *                                               'n' for noload
  3306.  *                                               'o' for over
  3307.  *                                               'w' for data
  3308.  *                         'd' (apparently m88k for data)
  3309.  *                                               'x' for text
  3310.  * But if the argument is not a quoted string, treat it as a
  3311.  * subsegment number.
  3312.  */
  3313.  
  3314. void
  3315. obj_coff_section (ignore)
  3316.      int ignore;
  3317. {
  3318.   /* Strip out the section name */
  3319.   char *section_name, *name;
  3320.   char c;
  3321.   unsigned int exp;
  3322.   long flags;
  3323.  
  3324.   if (flag_mri)
  3325.     {
  3326.       char type;
  3327.  
  3328.       s_mri_sect (&type);
  3329.       flags = 0;
  3330.       if (type == 'C')
  3331.     flags = STYP_TEXT;
  3332.       else if (type == 'D')
  3333.     flags = STYP_DATA;
  3334.       segment_info[now_seg].scnhdr.s_flags |= flags;
  3335.  
  3336.       return;
  3337.     }
  3338.  
  3339.   section_name = input_line_pointer;
  3340.   c = get_symbol_end ();
  3341.  
  3342.   name = xmalloc (input_line_pointer - section_name + 1);
  3343.   strcpy (name, section_name);
  3344.  
  3345.   *input_line_pointer = c;
  3346.  
  3347.   exp = 0;
  3348.   flags = 0;
  3349.  
  3350.   SKIP_WHITESPACE ();
  3351.   if (*input_line_pointer == ',')
  3352.     {
  3353.       ++input_line_pointer;
  3354.       SKIP_WHITESPACE ();
  3355.  
  3356.       if (*input_line_pointer != '"')
  3357.     exp = get_absolute_expression ();
  3358.       else
  3359.     {
  3360.       ++input_line_pointer;
  3361.       while (*input_line_pointer != '"'
  3362.          && ! is_end_of_line[(unsigned char) *input_line_pointer])
  3363.         {
  3364.           switch (*input_line_pointer)
  3365.         {
  3366.         case 'b': flags |= STYP_BSS;    break;
  3367.         case 'i': flags |= STYP_INFO;   break;
  3368.         case 'l': flags |= STYP_LIB;    break;
  3369.         case 'n': flags |= STYP_NOLOAD; break;
  3370.         case 'o': flags |= STYP_OVER;   break;
  3371.         case 'd':
  3372.         case 'w': flags |= STYP_DATA;   break;
  3373.         case 'x': flags |= STYP_TEXT;   break;
  3374.         default:
  3375.           as_warn("unknown section attribute '%c'",
  3376.               *input_line_pointer);
  3377.           break;
  3378.         }
  3379.           ++input_line_pointer;
  3380.         }
  3381.       if (*input_line_pointer == '"')
  3382.         ++input_line_pointer;
  3383.     }
  3384.     }
  3385.  
  3386.   subseg_new (name, (subsegT) exp);
  3387.  
  3388.   segment_info[now_seg].scnhdr.s_flags |= flags;
  3389.  
  3390.   demand_empty_rest_of_line ();
  3391. }
  3392.  
  3393.  
  3394. static void
  3395. obj_coff_text (ignore)
  3396.      int ignore;
  3397. {
  3398.   subseg_new (".text", get_absolute_expression ());
  3399. }
  3400.  
  3401.  
  3402. static void
  3403. obj_coff_data (ignore)
  3404.      int ignore;
  3405. {
  3406.   if (flag_readonly_data_in_text)
  3407.     subseg_new (".text", get_absolute_expression () + 1000);
  3408.   else
  3409.     subseg_new (".data", get_absolute_expression ());
  3410. }
  3411.  
  3412. static void
  3413. obj_coff_bss (ignore)
  3414.      int ignore;
  3415. {
  3416.   if (*input_line_pointer == '\n')    /* .bss         */
  3417.     subseg_new(".bss", get_absolute_expression());
  3418.   else                    /* .bss id,expr        */
  3419.     obj_coff_lcomm(0);
  3420. }
  3421.  
  3422. static void
  3423. obj_coff_ident (ignore)
  3424.      int ignore;
  3425. {
  3426.   segT current_seg = now_seg;        /* save current seg    */
  3427.   subsegT current_subseg = now_subseg;
  3428.   subseg_new (".comment", 0);        /* .comment seg        */
  3429.   stringer (1);                /* read string        */
  3430.   subseg_set (current_seg, current_subseg);    /* restore current seg    */
  3431. }
  3432.  
  3433. void
  3434. c_symbol_merge (debug, normal)
  3435.      symbolS *debug;
  3436.      symbolS *normal;
  3437. {
  3438.   S_SET_DATA_TYPE (normal, S_GET_DATA_TYPE (debug));
  3439.   S_SET_STORAGE_CLASS (normal, S_GET_STORAGE_CLASS (debug));
  3440.  
  3441.   if (S_GET_NUMBER_AUXILIARY (debug) > S_GET_NUMBER_AUXILIARY (normal))
  3442.     {
  3443.       S_SET_NUMBER_AUXILIARY (normal, S_GET_NUMBER_AUXILIARY (debug));
  3444.     }                /* take the most we have */
  3445.  
  3446.   if (S_GET_NUMBER_AUXILIARY (debug) > 0)
  3447.     {
  3448.       memcpy ((char *) &normal->sy_symbol.ost_auxent[0],
  3449.           (char *) &debug->sy_symbol.ost_auxent[0],
  3450.           (unsigned int) (S_GET_NUMBER_AUXILIARY (debug) * AUXESZ));
  3451.     }                /* Move all the auxiliary information */
  3452.  
  3453.   /* Move the debug flags. */
  3454.   SF_SET_DEBUG_FIELD (normal, SF_GET_DEBUG_FIELD (debug));
  3455. }                /* c_symbol_merge() */
  3456.  
  3457. static int
  3458. c_line_new (symbol, paddr, line_number, frag)
  3459.      symbolS * symbol;
  3460.      long paddr;
  3461.      int line_number;
  3462.      fragS * frag;
  3463. {
  3464.   struct lineno_list *new_line =
  3465.   (struct lineno_list *) xmalloc (sizeof (struct lineno_list));
  3466.  
  3467.   segment_info_type *s = segment_info + now_seg;
  3468.   new_line->line.l_lnno = line_number;
  3469.  
  3470.   if (line_number == 0)
  3471.     {
  3472.       last_line_symbol = symbol;
  3473.       new_line->line.l_addr.l_symndx = (long) symbol;
  3474.     }
  3475.   else
  3476.     {
  3477.       new_line->line.l_addr.l_paddr = paddr;
  3478.     }
  3479.  
  3480.   new_line->frag = (char *) frag;
  3481.   new_line->next = (struct lineno_list *) NULL;
  3482.  
  3483.  
  3484.   if (s->lineno_list_head == (struct lineno_list *) NULL)
  3485.     {
  3486.       s->lineno_list_head = new_line;
  3487.     }
  3488.   else
  3489.     {
  3490.       s->lineno_list_tail->next = new_line;
  3491.     }
  3492.   s->lineno_list_tail = new_line;
  3493.   return LINESZ * s->scnhdr.s_nlnno++;
  3494. }
  3495.  
  3496. void
  3497. c_dot_file_symbol (filename)
  3498.      char *filename;
  3499. {
  3500.   symbolS *symbolP;
  3501.  
  3502.   symbolP = symbol_new (".file",
  3503.             SEG_DEBUG,
  3504.             0,
  3505.             &zero_address_frag);
  3506.  
  3507.   S_SET_STORAGE_CLASS (symbolP, C_FILE);
  3508.   S_SET_NUMBER_AUXILIARY (symbolP, 1);
  3509.  
  3510.   if (strlen (filename) > FILNMLEN)
  3511.     {
  3512.       /* Filename is too long to fit into an auxent,
  3513.      we stick it into the string table instead.  We keep
  3514.      a linked list of the filenames we find so we can emit
  3515.      them later.*/
  3516.       struct filename_list *f = ((struct filename_list *)
  3517.                  xmalloc (sizeof (struct filename_list)));
  3518.  
  3519.       f->filename = filename;
  3520.       f->next = 0;
  3521.  
  3522.       SA_SET_FILE_FNAME_ZEROS (symbolP, 0);
  3523.       SA_SET_FILE_FNAME_OFFSET (symbolP, 1);
  3524.  
  3525.       if (filename_list_tail) 
  3526.     filename_list_tail->next = f;
  3527.       else
  3528.     filename_list_head = f;
  3529.       filename_list_tail = f;      
  3530.     }
  3531.   else 
  3532.     {
  3533.       SA_SET_FILE_FNAME (symbolP, filename);
  3534.     }
  3535. #ifndef NO_LISTING
  3536.   {
  3537.     extern int listing;
  3538.     if (listing)
  3539.       {
  3540.     listing_source_file (filename);
  3541.       }
  3542.  
  3543.   }
  3544.  
  3545. #endif
  3546.   SF_SET_DEBUG (symbolP);
  3547.   S_SET_VALUE (symbolP, (valueT) previous_file_symbol);
  3548.  
  3549.   previous_file_symbol = symbolP;
  3550.  
  3551.   /* Make sure that the symbol is first on the symbol chain */
  3552.   if (symbol_rootP != symbolP)
  3553.     {
  3554.       if (symbolP == symbol_lastP)
  3555.     {
  3556.       symbol_lastP = symbol_lastP->sy_previous;
  3557.     }            /* if it was the last thing on the list */
  3558.  
  3559.       symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
  3560.       symbol_insert (symbolP, symbol_rootP, &symbol_rootP, &symbol_lastP);
  3561.       symbol_rootP = symbolP;
  3562.     }                /* if not first on the list */
  3563.  
  3564. }                /* c_dot_file_symbol() */
  3565.  
  3566. /*
  3567.  * Build a 'section static' symbol.
  3568.  */
  3569.  
  3570. symbolS *
  3571. c_section_symbol (name, idx)
  3572.      char *name;
  3573.      int idx;
  3574. {
  3575.   symbolS *symbolP;
  3576.  
  3577.   symbolP = symbol_new (name, idx,
  3578.             0,
  3579.             &zero_address_frag);
  3580.  
  3581.   S_SET_STORAGE_CLASS (symbolP, C_STAT);
  3582.   S_SET_NUMBER_AUXILIARY (symbolP, 1);
  3583.  
  3584.   SF_SET_STATICS (symbolP);
  3585.  
  3586. #ifdef TE_PE
  3587.   /* If the .linkonce pseudo-op was used for this section, we must
  3588.      store the information in the auxiliary entry for the section
  3589.      symbol.  */
  3590.   if (segment_info[idx].linkonce != LINKONCE_UNSET)
  3591.     {
  3592.       int type;
  3593.  
  3594.       switch (segment_info[idx].linkonce)
  3595.     {
  3596.     default:
  3597.       abort ();
  3598.     case LINKONCE_DISCARD:
  3599.       type = IMAGE_COMDAT_SELECT_ANY;
  3600.       break;
  3601.     case LINKONCE_ONE_ONLY:
  3602.       type = IMAGE_COMDAT_SELECT_NODUPLICATES;
  3603.       break;
  3604.     case LINKONCE_SAME_SIZE:
  3605.       type = IMAGE_COMDAT_SELECT_SAME_SIZE;
  3606.       break;
  3607.     case LINKONCE_SAME_CONTENTS:
  3608.       type = IMAGE_COMDAT_SELECT_EXACT_MATCH;
  3609.       break;
  3610.     }
  3611.  
  3612.       SYM_AUXENT (symbolP)->x_scn.x_comdat = type;
  3613.     }
  3614. #endif /* TE_PE */
  3615.  
  3616.   return symbolP;
  3617. }                /* c_section_symbol() */
  3618.  
  3619. static void
  3620. w_symbols (abfd, where, symbol_rootP)
  3621.      bfd * abfd;
  3622.      char *where;
  3623.      symbolS * symbol_rootP;
  3624. {
  3625.   symbolS *symbolP;
  3626.   unsigned int i;
  3627.  
  3628.   /* First fill in those values we have only just worked out */
  3629.   for (i = SEG_E0; i < SEG_LAST; i++)
  3630.     {
  3631.       symbolP = segment_info[i].dot;
  3632.       if (symbolP)
  3633.     {
  3634.       SA_SET_SCN_SCNLEN (symbolP, segment_info[i].scnhdr.s_size);
  3635.       SA_SET_SCN_NRELOC (symbolP, segment_info[i].scnhdr.s_nreloc);
  3636.       SA_SET_SCN_NLINNO (symbolP, segment_info[i].scnhdr.s_nlnno);
  3637.     }
  3638.     }
  3639.  
  3640.   /*
  3641.      * Emit all symbols left in the symbol chain.
  3642.      */
  3643.   for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
  3644.     {
  3645.       /* Used to save the offset of the name. It is used to point
  3646.            to the string in memory but must be a file offset. */
  3647.       register char *temp;
  3648.  
  3649.       /* We can't fix the lnnoptr field in yank_symbols with the other
  3650.          adjustments, because we have to wait until we know where they
  3651.          go in the file.  */
  3652.       if (SF_GET_ADJ_LNNOPTR (symbolP))
  3653.     {
  3654.       SA_GET_SYM_LNNOPTR (symbolP) +=
  3655.         segment_info[S_GET_SEGMENT (symbolP)].scnhdr.s_lnnoptr;
  3656.     }
  3657.  
  3658.       tc_coff_symbol_emit_hook (symbolP);
  3659.  
  3660.       temp = S_GET_NAME (symbolP);
  3661.       if (SF_GET_STRING (symbolP))
  3662.     {
  3663.       S_SET_OFFSET (symbolP, symbolP->sy_name_offset);
  3664.       S_SET_ZEROES (symbolP, 0);
  3665.     }
  3666.       else
  3667.     {
  3668.       memset (symbolP->sy_symbol.ost_entry.n_name, 0, SYMNMLEN);
  3669.       strncpy (symbolP->sy_symbol.ost_entry.n_name, temp, SYMNMLEN);
  3670.     }
  3671.       where = symbol_to_chars (abfd, where, symbolP);
  3672.       S_SET_NAME (symbolP, temp);
  3673.     }
  3674.  
  3675. }                /* w_symbols() */
  3676.  
  3677. static void
  3678. obj_coff_lcomm (ignore)
  3679.      int ignore;
  3680. {
  3681.   s_lcomm(0);
  3682.   return;
  3683. #if 0
  3684.   char *name;
  3685.   char c;
  3686.   int temp;
  3687.   char *p;
  3688.  
  3689.   symbolS *symbolP;
  3690.  
  3691.   name = input_line_pointer;
  3692.  
  3693.   c = get_symbol_end ();
  3694.   p = input_line_pointer;
  3695.   *p = c;
  3696.   SKIP_WHITESPACE ();
  3697.   if (*input_line_pointer != ',')
  3698.     {
  3699.       as_bad ("Expected comma after name");
  3700.       ignore_rest_of_line ();
  3701.       return;
  3702.     }
  3703.   if (*input_line_pointer == '\n')
  3704.     {
  3705.       as_bad ("Missing size expression");
  3706.       return;
  3707.     }
  3708.   input_line_pointer++;
  3709.   if ((temp = get_absolute_expression ()) < 0)
  3710.     {
  3711.       as_warn ("lcomm length (%d.) <0! Ignored.", temp);
  3712.       ignore_rest_of_line ();
  3713.       return;
  3714.     }
  3715.   *p = 0;
  3716.  
  3717.   symbolP = symbol_find_or_make(name);
  3718.  
  3719.   if (S_GET_SEGMENT(symbolP) == SEG_UNKNOWN &&
  3720.       S_GET_VALUE(symbolP) == 0)
  3721.     {
  3722.       if (! need_pass_2)
  3723.     {
  3724.       char *p;
  3725.       segT current_seg = now_seg;     /* save current seg     */
  3726.       subsegT current_subseg = now_subseg;
  3727.  
  3728.       subseg_set (SEG_E2, 1);
  3729.       symbolP->sy_frag = frag_now;
  3730.       p = frag_var(rs_org, 1, 1, (relax_substateT)0, symbolP,
  3731.                temp, (char *)0);
  3732.       *p = 0;
  3733.       subseg_set (current_seg, current_subseg); /* restore current seg */
  3734.       S_SET_SEGMENT(symbolP, SEG_E2);
  3735.       S_SET_STORAGE_CLASS(symbolP, C_STAT);
  3736.     }
  3737.     }
  3738.   else
  3739.     as_bad("Symbol %s already defined", name);
  3740.  
  3741.   demand_empty_rest_of_line();
  3742. #endif
  3743. }
  3744.  
  3745. static void
  3746. fixup_mdeps (frags, h, this_segment)
  3747.      fragS * frags;
  3748.      object_headers * h;
  3749.      segT this_segment;
  3750. {
  3751.   subseg_change (this_segment, 0);
  3752.   while (frags)
  3753.     {
  3754.       switch (frags->fr_type)
  3755.     {
  3756.     case rs_align:
  3757.     case rs_align_code:
  3758.     case rs_org:
  3759. #ifdef HANDLE_ALIGN
  3760.       HANDLE_ALIGN (frags);
  3761. #endif
  3762.       frags->fr_type = rs_fill;
  3763.       frags->fr_offset =
  3764.         ((frags->fr_next->fr_address - frags->fr_address - frags->fr_fix)
  3765.          / frags->fr_var);
  3766.       break;
  3767.     case rs_machine_dependent:
  3768.       md_convert_frag (h, this_segment, frags);
  3769.       frag_wane (frags);
  3770.       break;
  3771.     default:
  3772.       ;
  3773.     }
  3774.       frags = frags->fr_next;
  3775.     }
  3776. }
  3777.  
  3778. #if 1
  3779.  
  3780. #ifndef TC_FORCE_RELOCATION
  3781. #define TC_FORCE_RELOCATION(fix) 0
  3782. #endif
  3783.  
  3784. static void
  3785. fixup_segment (segP, this_segment_type)
  3786.      segment_info_type * segP;
  3787.      segT this_segment_type;
  3788. {
  3789.   register fixS * fixP;
  3790.   register symbolS *add_symbolP;
  3791.   register symbolS *sub_symbolP;
  3792.   long add_number;
  3793.   register int size;
  3794.   register char *place;
  3795.   register long where;
  3796.   register char pcrel;
  3797.   register fragS *fragP;
  3798.   register segT add_symbol_segment = absolute_section;
  3799.  
  3800.   for (fixP = segP->fix_root; fixP; fixP = fixP->fx_next)
  3801.     {
  3802.       fragP = fixP->fx_frag;
  3803.       know (fragP);
  3804.       where = fixP->fx_where;
  3805.       place = fragP->fr_literal + where;
  3806.       size = fixP->fx_size;
  3807.       add_symbolP = fixP->fx_addsy;
  3808.       sub_symbolP = fixP->fx_subsy;
  3809.       add_number = fixP->fx_offset;
  3810.       pcrel = fixP->fx_pcrel;
  3811.  
  3812.       /* We want function-relative stabs to work on systems which
  3813.      may use a relaxing linker; thus we must handle the sym1-sym2
  3814.      fixups function-relative stabs generates.
  3815.  
  3816.      Of course, if you actually enable relaxing in the linker, the
  3817.      line and block scoping information is going to be incorrect
  3818.      in some cases.  The only way to really fix this is to support
  3819.      a reloc involving the difference of two symbols.  */
  3820.       if (linkrelax
  3821.       && (!sub_symbolP || pcrel))
  3822.     continue;
  3823.  
  3824. #ifdef TC_I960
  3825.       if (fixP->fx_tcbit && SF_GET_CALLNAME (add_symbolP))
  3826.     {
  3827.       /* Relocation should be done via the associated 'bal' entry
  3828.          point symbol. */
  3829.  
  3830.       if (!SF_GET_BALNAME (tc_get_bal_of_call (add_symbolP)))
  3831.         {
  3832.           as_bad_where (fixP->fx_file, fixP->fx_line,
  3833.                 "No 'bal' entry point for leafproc %s",
  3834.                 S_GET_NAME (add_symbolP));
  3835.           continue;
  3836.         }
  3837.       fixP->fx_addsy = add_symbolP = tc_get_bal_of_call (add_symbolP);
  3838.     }
  3839. #endif
  3840.  
  3841.       if (add_symbolP != NULL
  3842.       && add_symbolP->sy_mri_common)
  3843.     {
  3844.       know (add_symbolP->sy_value.X_op == O_symbol);
  3845.       add_number += S_GET_VALUE (add_symbolP);
  3846.       fixP->fx_offset = add_number;
  3847.       add_symbolP = fixP->fx_addsy = add_symbolP->sy_value.X_add_symbol;
  3848.     }
  3849.  
  3850.       if (add_symbolP)
  3851.     {
  3852.       add_symbol_segment = S_GET_SEGMENT (add_symbolP);
  3853.     }            /* if there is an addend */
  3854.  
  3855.       if (sub_symbolP)
  3856.     {
  3857.       if (add_symbolP == NULL || add_symbol_segment == absolute_section)
  3858.         {
  3859.           if (add_symbolP != NULL)
  3860.         {
  3861.           add_number += S_GET_VALUE (add_symbolP);
  3862.           add_symbolP = NULL;
  3863.           fixP->fx_addsy = NULL;
  3864.         }
  3865.  
  3866.           /* It's just -sym.  */
  3867.           if (S_GET_SEGMENT (sub_symbolP) == absolute_section)
  3868.         {
  3869.           add_number -= S_GET_VALUE (sub_symbolP);
  3870.           fixP->fx_subsy = 0;
  3871.           fixP->fx_done = 1;
  3872.         }
  3873.           else
  3874.         {
  3875. #ifndef TC_M68K
  3876.           as_bad_where (fixP->fx_file, fixP->fx_line,
  3877.                 "Negative of non-absolute symbol %s",
  3878.                 S_GET_NAME (sub_symbolP));
  3879. #endif
  3880.           add_number -= S_GET_VALUE (sub_symbolP);
  3881.         }        /* not absolute */
  3882.  
  3883.           /* if sub_symbol is in the same segment that add_symbol
  3884.          and add_symbol is either in DATA, TEXT, BSS or ABSOLUTE */
  3885.         }
  3886.       else if (S_GET_SEGMENT (sub_symbolP) == add_symbol_segment
  3887.            && SEG_NORMAL (add_symbol_segment))
  3888.         {
  3889.           /* Difference of 2 symbols from same segment.  Can't
  3890.          make difference of 2 undefineds: 'value' means
  3891.          something different for N_UNDF. */
  3892. #ifdef TC_I960
  3893.           /* Makes no sense to use the difference of 2 arbitrary symbols
  3894.              as the target of a call instruction.  */
  3895.           if (fixP->fx_tcbit)
  3896.         {
  3897.           as_bad_where (fixP->fx_file, fixP->fx_line,
  3898.                 "callj to difference of 2 symbols");
  3899.         }
  3900. #endif /* TC_I960 */
  3901.           add_number += S_GET_VALUE (add_symbolP) -
  3902.         S_GET_VALUE (sub_symbolP);
  3903.           add_symbolP = NULL;
  3904.  
  3905.           if (!TC_FORCE_RELOCATION (fixP))
  3906.         {
  3907.           fixP->fx_addsy = NULL;
  3908.           fixP->fx_subsy = NULL;
  3909.           fixP->fx_done = 1;
  3910. #ifdef TC_M68K /* is this right? */
  3911.           pcrel = 0;
  3912.           fixP->fx_pcrel = 0;
  3913. #endif
  3914.         }
  3915.         }
  3916.       else
  3917.         {
  3918.           /* Different segments in subtraction. */
  3919.           know (!(S_IS_EXTERNAL (sub_symbolP) && (S_GET_SEGMENT (sub_symbolP) == absolute_section)));
  3920.  
  3921.           if ((S_GET_SEGMENT (sub_symbolP) == absolute_section))
  3922.         {
  3923.           add_number -= S_GET_VALUE (sub_symbolP);
  3924.         }
  3925. #ifdef DIFF_EXPR_OK
  3926.           else if (S_GET_SEGMENT (sub_symbolP) == this_segment_type
  3927. #if 0 /* Okay for 68k, at least... */
  3928.                && !pcrel
  3929. #endif
  3930.                )
  3931.         {
  3932.           /* Make it pc-relative.  */
  3933.           add_number += (md_pcrel_from (fixP)
  3934.                  - S_GET_VALUE (sub_symbolP));
  3935.           pcrel = 1;
  3936.           fixP->fx_pcrel = 1;
  3937.           sub_symbolP = 0;
  3938.           fixP->fx_subsy = 0;
  3939.         }
  3940. #endif
  3941.           else
  3942.         {
  3943.           as_bad_where (fixP->fx_file, fixP->fx_line,
  3944.                 "Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %ld.",
  3945.                 segment_name (S_GET_SEGMENT (sub_symbolP)),
  3946.                 S_GET_NAME (sub_symbolP),
  3947.                 (long) (fragP->fr_address + where));
  3948.         }        /* if absolute */
  3949.         }
  3950.     }            /* if sub_symbolP */
  3951.  
  3952.       if (add_symbolP)
  3953.     {
  3954.       if (add_symbol_segment == this_segment_type && pcrel)
  3955.         {
  3956.           /*
  3957.            * This fixup was made when the symbol's segment was
  3958.            * SEG_UNKNOWN, but it is now in the local segment.
  3959.            * So we know how to do the address without relocation.
  3960.            */
  3961. #ifdef TC_I960
  3962.           /* reloc_callj() may replace a 'call' with a 'calls' or a 'bal',
  3963.            * in which cases it modifies *fixP as appropriate.  In the case
  3964.            * of a 'calls', no further work is required, and *fixP has been
  3965.            * set up to make the rest of the code below a no-op.
  3966.            */
  3967.           reloc_callj (fixP);
  3968. #endif /* TC_I960 */
  3969.  
  3970.           add_number += S_GET_VALUE (add_symbolP);
  3971.           add_number -= md_pcrel_from (fixP);
  3972. #if defined (TC_I386) || defined (TE_LYNX)
  3973.           /* On the 386 we must adjust by the segment
  3974.          vaddr as well.  Ian Taylor.  */
  3975.           add_number -= segP->scnhdr.s_vaddr;
  3976. #endif
  3977.           pcrel = 0;    /* Lie. Don't want further pcrel processing. */
  3978.           if (!TC_FORCE_RELOCATION (fixP))
  3979.         {
  3980.           fixP->fx_addsy = NULL;
  3981.           fixP->fx_done = 1;
  3982.         }
  3983.         }
  3984.       else
  3985.         {
  3986.           switch (add_symbol_segment)
  3987.         {
  3988.         case absolute_section:
  3989. #ifdef TC_I960
  3990.           reloc_callj (fixP);    /* See comment about reloc_callj() above*/
  3991. #endif /* TC_I960 */
  3992.           add_number += S_GET_VALUE (add_symbolP);
  3993.           add_symbolP = NULL;
  3994.  
  3995.           if (!TC_FORCE_RELOCATION (fixP))
  3996.             {
  3997.               fixP->fx_addsy = NULL;
  3998.               fixP->fx_done = 1;
  3999.             }
  4000.           break;
  4001.         default:
  4002.  
  4003.  
  4004. #if defined(TC_A29K) || (defined(TE_PE) && defined(TC_I386)) || defined(TC_M88K)
  4005.           /* This really should be handled in the linker, but
  4006.              backward compatibility forbids.  */
  4007.           add_number += S_GET_VALUE (add_symbolP);
  4008. #else
  4009.           add_number += S_GET_VALUE (add_symbolP) +
  4010.             segment_info[S_GET_SEGMENT (add_symbolP)].scnhdr.s_paddr;
  4011. #endif
  4012.           break;
  4013.  
  4014.         case SEG_UNKNOWN:
  4015. #ifdef TC_I960
  4016.           if ((int) fixP->fx_bit_fixP == 13)
  4017.             {
  4018.               /* This is a COBR instruction.  They have only a
  4019.                * 13-bit displacement and are only to be used
  4020.                * for local branches: flag as error, don't generate
  4021.                * relocation.
  4022.                */
  4023.               as_bad_where (fixP->fx_file, fixP->fx_line,
  4024.                     "can't use COBR format with external label");
  4025.               fixP->fx_addsy = NULL;
  4026.               fixP->fx_done = 1;
  4027.               continue;
  4028.             }        /* COBR */
  4029. #endif /* TC_I960 */
  4030. #if (defined (TC_I386) || defined (TE_LYNX) || defined (TE_AUX)) && !defined(TE_PE)
  4031.           /* 386 COFF uses a peculiar format in which the
  4032.              value of a common symbol is stored in the .text
  4033.              segment (I've checked this on SVR3.2 and SCO
  4034.              3.2.2) Ian Taylor <ian@cygnus.com>.  */
  4035.           if (S_IS_COMMON (add_symbolP))
  4036.             add_number += S_GET_VALUE (add_symbolP);
  4037. #endif
  4038.           break;
  4039.  
  4040.  
  4041.         }        /* switch on symbol seg */
  4042.         }            /* if not in local seg */
  4043.     }            /* if there was a + symbol */
  4044.  
  4045.       if (pcrel)
  4046.     {
  4047. #if !defined(TC_M88K) && !(defined(TE_PE) && defined(TC_I386)) && !defined(TC_A29K)
  4048.       /* This adjustment is not correct on the m88k, for which the
  4049.          linker does all the computation.  */
  4050.       add_number -= md_pcrel_from (fixP);
  4051. #endif
  4052.       if (add_symbolP == 0)
  4053.         {
  4054.           fixP->fx_addsy = &abs_symbol;
  4055.         }            /* if there's an add_symbol */
  4056. #if defined (TC_I386) || defined (TE_LYNX)
  4057.       /* On the 386 we must adjust by the segment vaddr
  4058.          as well.  Ian Taylor.  */
  4059.       add_number -= segP->scnhdr.s_vaddr;
  4060. #endif
  4061.     }            /* if pcrel */
  4062.  
  4063.       if (!fixP->fx_bit_fixP)
  4064.     {
  4065. #ifndef TC_M88K
  4066.       /* The m88k uses the offset field of the reloc to get around
  4067.          this problem.  */
  4068.       if ((size == 1
  4069.            && (add_number & ~0xFF)
  4070.            && ((add_number & ~0xFF) != (-1 & ~0xFF)))
  4071.           || (size == 2
  4072.           && (add_number & ~0xFFFF)
  4073.           && ((add_number & ~0xFFFF) != (-1 & ~0xFFFF))))
  4074.         {
  4075.           as_bad_where (fixP->fx_file, fixP->fx_line,
  4076.                 "Value of %ld too large for field of %d bytes at 0x%lx",
  4077.                 (long) add_number, size,
  4078.                 (unsigned long) (fragP->fr_address + where));
  4079.         }
  4080. #endif
  4081. #ifdef WARN_SIGNED_OVERFLOW_WORD
  4082.       /* Warn if a .word value is too large when treated as a
  4083.          signed number.  We already know it is not too negative.
  4084.          This is to catch over-large switches generated by gcc on
  4085.          the 68k.  */
  4086.       if (!flag_signed_overflow_ok
  4087.           && size == 2
  4088.           && add_number > 0x7fff)
  4089.         as_bad_where (fixP->fx_file, fixP->fx_line,
  4090.               "Signed .word overflow; switch may be too large; %ld at 0x%lx",
  4091.               (long) add_number,
  4092.               (unsigned long) (fragP->fr_address + where));
  4093. #endif
  4094.     }            /* not a bit fix */
  4095.       /* Once this fix has been applied, we don't have to output
  4096.      anything nothing more need be done.  */
  4097. #ifdef MD_APPLY_FIX3
  4098.       md_apply_fix3 (fixP, &add_number, this_segment_type);
  4099. #else
  4100.       md_apply_fix (fixP, add_number);
  4101. #endif
  4102.     }                /* For each fixS in this segment. */
  4103. }                /* fixup_segment() */
  4104.  
  4105. #endif
  4106.  
  4107. /* The first entry in a .stab section is special.  */
  4108.  
  4109. void
  4110. obj_coff_init_stab_section (seg)
  4111.      segT seg;
  4112. {
  4113.   char *file;
  4114.   char *p;
  4115.   char *stabstr_name;
  4116.   unsigned int stroff;
  4117.  
  4118.   /* Make space for this first symbol. */
  4119.   p = frag_more (12);
  4120.   /* Zero it out. */
  4121.   memset (p, 0, 12);
  4122.   as_where (&file, (unsigned int *) NULL);
  4123.   stabstr_name = (char *) alloca (strlen (segment_info[seg].name) + 4);
  4124.   strcpy (stabstr_name, segment_info[seg].name);
  4125.   strcat (stabstr_name, "str");
  4126.   stroff = get_stab_string_offset (file, stabstr_name);
  4127.   know (stroff == 1);
  4128.   md_number_to_chars (p, stroff, 4);
  4129. }
  4130.  
  4131. /* Fill in the counts in the first entry in a .stab section.  */
  4132.  
  4133. static void
  4134. adjust_stab_section(abfd, seg)
  4135.      bfd *abfd;
  4136.      segT seg;
  4137. {
  4138.   segT stabstrseg = SEG_UNKNOWN;
  4139.   const char *secname, *name2;
  4140.   char *name;
  4141.   char *p = NULL;
  4142.   int i, strsz = 0, nsyms;
  4143.   fragS *frag = segment_info[seg].frchainP->frch_root;
  4144.  
  4145.   /* Look for the associated string table section. */
  4146.  
  4147.   secname = segment_info[seg].name;
  4148.   name = (char *) alloca (strlen (secname) + 4);
  4149.   strcpy (name, secname);
  4150.   strcat (name, "str");
  4151.  
  4152.   for (i = SEG_E0; i < SEG_UNKNOWN; i++)
  4153.     {
  4154.       name2 = segment_info[i].name;
  4155.       if (name2 != NULL && strncmp(name2, name, 8) == 0)
  4156.     {
  4157.       stabstrseg = i;
  4158.       break;
  4159.     }
  4160.     }
  4161.  
  4162.   /* If we found the section, get its size. */
  4163.   if (stabstrseg != SEG_UNKNOWN)
  4164.     strsz = size_section (abfd, stabstrseg);
  4165.  
  4166.   nsyms = size_section (abfd, seg) / 12 - 1;
  4167.  
  4168.   /* Look for the first frag of sufficient size for the initial stab
  4169.      symbol, and collect a pointer to it. */
  4170.   while (frag && frag->fr_fix < 12)
  4171.     frag = frag->fr_next;
  4172.   assert (frag != 0);
  4173.   p = frag->fr_literal;
  4174.   assert (p != 0);
  4175.  
  4176.   /* Write in the number of stab symbols and the size of the string
  4177.      table. */
  4178.   bfd_h_put_16 (abfd, (bfd_vma) nsyms, (bfd_byte *) p + 6);
  4179.   bfd_h_put_32 (abfd, (bfd_vma) strsz, (bfd_byte *) p + 8);
  4180. }
  4181.  
  4182. #endif /* not BFD_ASSEMBLER */
  4183.  
  4184. const pseudo_typeS obj_pseudo_table[] =
  4185. {
  4186.   {"def", obj_coff_def, 0},
  4187.   {"dim", obj_coff_dim, 0},
  4188.   {"endef", obj_coff_endef, 0},
  4189.   {"line", obj_coff_line, 0},
  4190.   {"ln", obj_coff_ln, 0},
  4191.   {"appline", obj_coff_ln, 1},
  4192.   {"scl", obj_coff_scl, 0},
  4193.   {"size", obj_coff_size, 0},
  4194.   {"tag", obj_coff_tag, 0},
  4195.   {"type", obj_coff_type, 0},
  4196.   {"val", obj_coff_val, 0},
  4197.   {"section", obj_coff_section, 0},
  4198.   {"sect", obj_coff_section, 0},
  4199.   /* FIXME: We ignore the MRI short attribute.  */
  4200.   {"section.s", obj_coff_section, 0},
  4201.   {"sect.s", obj_coff_section, 0},
  4202. #ifndef BFD_ASSEMBLER
  4203.   {"use", obj_coff_section, 0},
  4204.   {"text", obj_coff_text, 0},
  4205.   {"data", obj_coff_data, 0},
  4206.   {"bss", obj_coff_bss, 0},
  4207.   {"lcomm", obj_coff_lcomm, 0},
  4208.   {"ident", obj_coff_ident, 0},
  4209. #else
  4210.   {"optim", s_ignore, 0},    /* For sun386i cc (?) */
  4211.   {"ident", s_ignore, 0},    /* we don't yet handle this. */
  4212. #endif
  4213.   {"ABORT", s_abort, 0},
  4214. #ifdef TC_M88K
  4215.   /* The m88k uses sdef instead of def.  */
  4216.   {"sdef", obj_coff_def, 0},
  4217. #endif
  4218.   {NULL}            /* end sentinel */
  4219. };                /* obj_pseudo_table */
  4220.